/// <summary> /// Returns the coordinates of the window and client area (including the Main Menu) /// relative to the screen. /// </summary> /// <remarks> /// This method exists since some windows (e.g., Calculator main window) report /// a window size that is smaller than it really is. /// </remarks> public static bool GetWindowRectangles( Logger logger, IntPtr window, out SafeNativeMethods.RECT windowRectangle, out SafeNativeMethods.RECT clientRectangle) { string traceMsg = Tracer.FormatCall("GetWindowRectangles", window); ArgumentGuard.NotNull(logger, nameof(logger)); try { logger.Log(traceMsg); windowRectangle = clientRectangle = new SafeNativeMethods.RECT(); SafeNativeMethods.WINDOWINFO wi = new SafeNativeMethods.WINDOWINFO(); wi.cbSize = (uint)Marshal.SizeOf(typeof(SafeNativeMethods.WINDOWINFO)); if (!SafeNativeMethods.GetWindowInfo(window, ref wi)) { logger.Log( "{0}: GetWindowInfo failed ({1})", traceMsg, Marshal.GetLastWin32Error()); return(false); } windowRectangle = wi.rcWindow; clientRectangle = wi.rcClient; // We assume that border width and height is symmetric... const SafeNativeMethods.WindowStyles CaptionFlag = SafeNativeMethods.WindowStyles.WS_CAPTION; if (CaptionFlag == (wi.dwStyle & CaptionFlag)) { var border = (windowRectangle.Width - clientRectangle.Width) / 2; clientRectangle.Top = windowRectangle.Top + border + SystemInformation.CaptionHeight; } logger.Verbose("{0} succeeded", traceMsg); return(true); } catch (Exception ex) { logger.Log("{0} failed: {1}", traceMsg, ex); throw; } }
private void PeriodicActionHandler_() { if (!SafeNativeMethods.GetGUIThreadInfo(0, ref guiThreadInfo_)) { return; } // report negative events bool reportFocus = ReportExpired_( ref focus_, guiThreadInfo_.hwndFocus, GuiEventTypes.WindowLostFocus); bool reportActive = ReportExpired_( ref active_, guiThreadInfo_.hwndActive, GuiEventTypes.WindowDeactivated); // report positive events FireWindowEvent_(reportActive, GuiEventTypes.WindowActivated, active_); FireWindowEvent_(reportFocus, GuiEventTypes.WindowGotFocus, focus_); // handle move & size if (reportActive) { SafeNativeMethods.GetWindowRect(active_, out prev_); } else if (IntPtr.Zero != active_) { // the active window did not change, unless it is still moving / resizing // lets check if its changed location or position const uint GUI_INMOVESIZE = 0x2; if (0 == (guiThreadInfo_.flags & GUI_INMOVESIZE)) { SafeNativeMethods.RECT current = new SafeNativeMethods.RECT(); SafeNativeMethods.GetWindowRect(active_, out current); FireWindowEvent_( prev_.Left != current.Left || prev_.Top != current.Top, GuiEventTypes.WindowMoved, active_); FireWindowEvent_( prev_.Width != current.Width || prev_.Height != current.Height, GuiEventTypes.WindowResized, active_); prev_ = current; } } }
private void ResetState_() { active_ = IntPtr.Zero; focus_ = IntPtr.Zero; prev_ = new SafeNativeMethods.RECT(); }