private static Rectangle GetFrameRect(IntPtr hWnd) { Rectangle windowRect; User32Interop.RECT rect; User32Interop.POINT point = new User32Interop.POINT(0, 0); User32Interop.WINDOWPLACEMENT wp = User32Interop.WINDOWPLACEMENT.Default; // Get window border information User32Interop.WINDOWINFO wi = new User32Interop.WINDOWINFO(null); User32Interop.GetWindowInfo(hWnd, ref wi); // Get window placement information (maximized, minimized, etc) User32Interop.GetWindowPlacement(hWnd, out wp); // Get window rectangle User32Interop.GetWindowRect(hWnd, out rect); if (wp.ShowCmd == User32Interop.ShowWindowCommands.Maximize) { rect.left += (int)wi.cxWindowBorders; rect.top += (int)wi.cyWindowBorders; rect.right -= (int)wi.cxWindowBorders; rect.bottom -= (int)wi.cyWindowBorders; windowRect = new Rectangle(rect.left, rect.top, rect.right, rect.bottom); } else { windowRect = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); } return(windowRect); }
public IntPtr Find() { if (!this.isFinding) { return(IntPtr.Zero); } IntPtr hNewWnd; User32Interop.POINT pt = new User32Interop.POINT(); if (false == User32Interop.GetCursorPos(out pt)) { hNewWnd = IntPtr.Zero; } else { hNewWnd = User32Interop.WindowFromPoint(pt); } // Check if it is the previous handle if (hNewWnd == this.hWnd) { Debug.WriteLine("WindowFinder Process: [Same]"); return(hNewWnd); } // Make sure the window does not belog to current process if (hNewWnd != IntPtr.Zero) { uint processId; User32Interop.GetWindowThreadProcessId(hNewWnd, out processId); int currentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id; Debug.WriteLineIf(processId != 0, "WindowFinder Process: " + Process.GetProcessById((int)processId).ProcessName); if (processId == currentProcessId) { hNewWnd = IntPtr.Zero; } } // Clear previous frame if (this.hWnd != IntPtr.Zero) { DrawFrame(this.hWnd); } if (hNewWnd != IntPtr.Zero) { // Get window text this.text = GetText(hNewWnd); // Draw new frame DrawFrame(hNewWnd); } else { this.text = string.Empty; } // Return new handle this.hWnd = hNewWnd; return(this.hWnd); }