/// <summary> /// Remove the window from "Alt + TAB list". /// </summary> /// <param name="window">The window</param> public static void WindowMissFromMission(Window window) { WindowInteropHelper helper = new WindowInteropHelper(window); long old = WinAPIWrapper.GetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE); old |= WinAPIWrapper.WS_EX_TOOLWINDOW; Console.WriteLine(WinAPIWrapper.SetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE, (IntPtr)old)); }
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); IntPtr handle = (new WindowInteropHelper(this)).Handle; HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WindowProc)); taskBarCreatedMsg = WinAPIWrapper.RegisterWindowMessage("TaskbarCreated"); uCallBackMsg = WinAPIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER_USTC.Software.hanyizhao.NetSpeedMonitor"); RegisterAppBar(true); }
/// <summary> /// Remove the window from "Alt + TAB list". /// </summary> /// <param name="window">The window</param> /// <param name="NoActivate">No activate (get focus)</param> public static void WindowMissFromMission(Window window, bool NoActivate) { WindowInteropHelper helper = new WindowInteropHelper(window); long old = WinAPIWrapper.GetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE); old |= WinAPIWrapper.WS_EX_TOOLWINDOW; if (NoActivate) { old |= WinAPIWrapper.WS_EX_NOACTIVATE; } Console.WriteLine("Remove the window from Alt+TAB list" + WinAPIWrapper.SetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE, (IntPtr)old)); }
/// <summary> /// Call this method in main thread. /// </summary> /// <param name="causeNotFillScreen">The foreground window is not a full screen App because it doesn't fill the screen. /// Otherwise, because it is Window Explorer or...</param> /// <returns></returns> private bool CheckHasFullScreenApp(out bool causeNotFillScreen) { causeNotFillScreen = false; bool result; IntPtr foreWindow = WinAPIWrapper.GetForegroundWindow(); WinAPIWrapper.GetWindowThreadProcessId(foreWindow, out uint processid); String foreGroundWindowName = ""; try { foreGroundWindowName = Process.GetProcessById((int)processid).ProcessName; //Console.WriteLine("foreGroundWindowName:" + foreGroundWindowName); } catch (Exception) { } if (foreGroundWindowName != "explorer" && !foreWindow.Equals(new WindowInteropHelper(this).Handle)) { IntPtr deskWindow = WinAPIWrapper.GetDesktopWindow(); if (!foreWindow.Equals(deskWindow) && !foreWindow.Equals(WinAPIWrapper.GetShellWindow())) { WinAPIWrapper.GetWindowRect(foreWindow, out RECT foreWindowRECT); WinAPIWrapper.GetWindowRect(deskWindow, out RECT deskWindowRECT); //Console.WriteLine("foreWindow RECT:" + foreWindowRECT); //Console.WriteLine("deskWindow RECT:" + deskWindowRECT); // Check whether foreground Window fills main screen. result = foreWindowRECT.left <= deskWindowRECT.left && foreWindowRECT.top <= deskWindowRECT.top && foreWindowRECT.right >= deskWindowRECT.right && foreWindowRECT.bottom >= deskWindowRECT.bottom; causeNotFillScreen = true; } else { // Foreground Window is DeskWindow or ShellWindow. result = false; } } else { // Foreground window is Windows Explorer or MainWindow itself. result = false; } return(result); }
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == uCallBackMsg) { if (wParam.ToInt32() == (int)ABNotify.ABN_FULLSCREENAPP) { IntPtr win = WinAPIWrapper.GetForegroundWindow(); if (!win.Equals(desktopHandle) && !win.Equals(shellHandle) && lParam.ToInt32() == 1) { HideAllView(true); } else { HideAllView(false); } } } return(IntPtr.Zero); }
public void RegisterAppBar(bool register) { APPBARDATA abd = new APPBARDATA(); abd.cbSize = Marshal.SizeOf(abd); abd.hWnd = new WindowInteropHelper(this).Handle; if (register) { //register abd.uCallbackMessage = uCallBackMsg; uint ret = WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd); // Check whether there is a full screen app now. HideAllView(CheckHasFullScreenApp(out bool a)); } else { WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd); } }
private static void OnWindowHeightAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { if (d is Window window) { IntPtr handle = new WindowInteropHelper(window).Handle; if (WinAPIWrapper.GetWindowRect(handle, out RECT rect)) { if (PresentationSource.FromVisual(window) != null) { int height = (int)Math.Round(window.PointToScreen(new Point(0, (double)e.NewValue)).Y - rect.top); WinAPIWrapper.SetWindowPos(handle, new IntPtr((int)SpecialWindowHandles.TopMost), rect.left, rect.top, rect.right - rect.left, height, SetWindowPosFlags.SHOWWINDOW); } } } } catch (Exception) { } }
public void RegisterAppBar(bool register) { APPBARDATA abd = new APPBARDATA(); abd.cbSize = Marshal.SizeOf(abd); WindowInteropHelper helper = new WindowInteropHelper(this); abd.hWnd = helper.Handle; if (register) { //register uCallBackMsg = WinAPIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER_USTC.Software.hanyizhao.NetSpeedMonitor"); abd.uCallbackMessage = uCallBackMsg; uint ret = WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd); } else { WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd); } }
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == uCallBackMsg) { if (wParam.ToInt32() == (int)ABNotify.ABN_FULLSCREENAPP) { bool hasFull = false; if (lParam.ToInt32() == 1) { IntPtr foreWindow = WinAPIWrapper.GetForegroundWindow(); WinAPIWrapper.GetWindowThreadProcessId(foreWindow, out uint processid); String foreGroundWindowName = ""; try { foreGroundWindowName = Process.GetProcessById((int)processid).ProcessName; } catch (Exception) { } if (foreGroundWindowName != "explorer" && !foreWindow.Equals(new WindowInteropHelper(this).Handle)) { IntPtr deskWindow = WinAPIWrapper.GetDesktopWindow(); if (!foreWindow.Equals(deskWindow) && !foreWindow.Equals(WinAPIWrapper.GetShellWindow())) { WinAPIWrapper.GetWindowRect(foreWindow, out RECT foreWindowRECT); WinAPIWrapper.GetWindowRect(deskWindow, out RECT deskWindowRECT); hasFull = foreWindowRECT.left <= deskWindowRECT.left && foreWindowRECT.top <= deskWindowRECT.top && foreWindowRECT.right >= deskWindowRECT.right && foreWindowRECT.bottom >= deskWindowRECT.bottom; } } } HideAllView(hasFull); } } return(IntPtr.Zero); }
public void RegisterAppBar(bool register) { APPBARDATA abd = new APPBARDATA(); abd.cbSize = Marshal.SizeOf(abd); WindowInteropHelper helper = new WindowInteropHelper(this); abd.hWnd = helper.Handle; desktopHandle = WinAPIWrapper.GetDesktopWindow(); shellHandle = WinAPIWrapper.GetShellWindow(); if (register) { //register uCallBackMsg = WinAPIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER"); abd.uCallbackMessage = uCallBackMsg; uint ret = WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd); } else { WinAPIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd); } }
/// <summary> /// This function reads and parses the active TCP socket connections available /// and stores them in a list. /// </summary> /// <returns> /// It returns the current set of TCP socket connections which are active. /// </returns> /// <exception cref="OutOfMemoryException"> /// This exception may be thrown by the function Marshal.AllocHGlobal when there /// is insufficient memory to satisfy the request. /// </exception> public static List <TcpProcessRecord> GetAllTcpConnections() { int bufferSize = 0; List <TcpProcessRecord> tcpTableRecords = new List <TcpProcessRecord>(100); // Getting the size of TCP table, that is returned in 'bufferSize' variable. uint result = WinAPIWrapper.GetExtendedTcpTable(IntPtr.Zero, ref bufferSize, true, AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL); // Allocating memory from the unmanaged memory of the process by using the // specified number of bytes in 'bufferSize' variable. IntPtr tcpTableRecordsPtr = Marshal.AllocHGlobal(bufferSize); try { // The size of the table returned in 'bufferSize' variable in previous // call must be used in this subsequent call to 'GetExtendedTcpTable' // function in order to successfully retrieve the table. result = WinAPIWrapper.GetExtendedTcpTable(tcpTableRecordsPtr, ref bufferSize, true, AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL); // Non-zero value represent the function 'GetExtendedTcpTable' failed, // hence empty list is returned to the caller function. if (result != 0) { return(new List <TcpProcessRecord>()); } // Marshals data from an unmanaged block of memory to a newly allocated // managed object 'tcpRecordsTable' of type 'MIB_TCPTABLE_OWNER_PID' // to get number of entries of the specified TCP table structure. MIB_TCPTABLE_OWNER_PID tcpRecordsTable = (MIB_TCPTABLE_OWNER_PID) Marshal.PtrToStructure(tcpTableRecordsPtr, typeof(MIB_TCPTABLE_OWNER_PID)); IntPtr tableRowPtr = (IntPtr)((long)tcpTableRecordsPtr + Marshal.SizeOf(tcpRecordsTable.dwNumEntries)); // Reading and parsing the TCP records one by one from the table and // storing them in a list of 'TcpProcessRecord' structure type objects. for (int row = 0; row < tcpRecordsTable.dwNumEntries; row++) { MIB_TCPROW_OWNER_PID tcpRow = (MIB_TCPROW_OWNER_PID)Marshal. PtrToStructure(tableRowPtr, typeof(MIB_TCPROW_OWNER_PID)); tcpTableRecords.Add(new TcpProcessRecord(tcpRow.localAddr, tcpRow.remoteAddr, BitConverter.ToUInt16(new byte[2] { tcpRow.localPort[1], tcpRow.localPort[0] }, 0), BitConverter.ToUInt16(new byte[2] { tcpRow.remotePort[1], tcpRow.remotePort[0] }, 0), tcpRow.owningPid, tcpRow.state)); tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(tcpRow)); } } catch (OutOfMemoryException outOfMemoryException) { } catch (Exception exception) { } finally { Marshal.FreeHGlobal(tcpTableRecordsPtr); } return(tcpTableRecords != null?tcpTableRecords.Distinct() .ToList <TcpProcessRecord>() : new List <TcpProcessRecord>()); }