private void setupInstance(IntPtr parentWindowHandle, Direct2DRendererOptions options) { ParentWindowHandle = parentWindowHandle; if (PInvoke.IsWindow(parentWindowHandle) == 0) { throw new Exception("The parent window does not exist"); } PInvoke.RECT bounds = new PInvoke.RECT(); PInvoke.GetRealWindowRect(parentWindowHandle, out bounds); int x = bounds.Left; int y = bounds.Top; int width = bounds.Right - x; int height = bounds.Bottom - y; Window = new OverlayWindow(x, y, width, height); options.Hwnd = Window.WindowHandle; Graphics = new Direct2DRenderer(options); serviceThread = new Thread(new ThreadStart(windowServiceThread)) { IsBackground = true, Priority = ThreadPriority.BelowNormal }; serviceThread.Start(); }
private void ClipOrCenterRectToMonitorWin32(ref PInvoke.RECT prc, bool UseWorkArea, bool IsCenter) { IntPtr hMonitor; PInvoke.RECT rc; int w = prc.right - prc.left; int h = prc.bottom - prc.top; hMonitor = PInvoke.User32.MonitorFromRect(ref prc, PInvoke.User32.MonitorOptions.MONITOR_DEFAULTTONEAREST); PInvoke.User32.MONITORINFO mi = new PInvoke.User32.MONITORINFO(); mi.cbSize = Marshal.SizeOf(mi); PInvoke.User32.GetMonitorInfo(hMonitor, ref mi); rc = UseWorkArea ? mi.rcWork : mi.rcMonitor; if (IsCenter) { prc.left = rc.left + (rc.right - rc.left - w) / 2; prc.top = rc.top + (rc.bottom - rc.top - h) / 2; prc.right = prc.left + w; prc.bottom = prc.top + h; } else { prc.left = Math.Max(rc.left, Math.Min(rc.right - w, prc.left)); prc.top = Math.Max(rc.top, Math.Min(rc.bottom - h, prc.top)); prc.right = prc.left + w; prc.bottom = prc.top + h; } }
static void GetControlRect(IntPtr hWnd, IntPtr controlHWND, out PInvoke.RECT rect) { PInvoke.GetWindowRect(controlHWND, out rect); var pt = new PInvoke.POINT(rect.Left, rect.Top); PInvoke.ScreenToClient(hWnd, ref pt); rect.Right = rect.Right - rect.Left + pt.X; rect.Bottom = rect.Bottom - rect.Top + pt.Y; rect.Left = pt.X; rect.Top = pt.Y; }
private void windowServiceThread() { PInvoke.RECT bounds = new PInvoke.RECT(); while (!exitThread) { Thread.Sleep(100); if (PInvoke.IsWindowVisible(ParentWindowHandle) == 0) { if (Window.IsVisible) { Window.HideWindow(); } continue; } if (!Window.IsVisible) { Window.ShowWindow(); } if (OverlayWindow.BypassTopmost) { IntPtr windowAboveParentWindow = PInvoke.GetWindow(ParentWindowHandle, 3 /* GW_HWNDPREV */); if (windowAboveParentWindow != Window.WindowHandle) { PInvoke.SetWindowPos(Window.WindowHandle, windowAboveParentWindow, 0, 0, 0, 0, 0x10 | 0x2 | 0x1 | 0x4000); // SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS } } PInvoke.GetRealWindowRect(ParentWindowHandle, out bounds); int x = bounds.Left; int y = bounds.Top; int width = bounds.Right - x; int height = bounds.Bottom - y; if (Window.X == x && Window.Y == y && Window.Width == width && Window.Height == height) { continue; } Window.SetWindowBounds(x, y, width, height); Graphics.Resize(width, height); } }
private void windowServiceThread() { PInvoke.RECT bounds = new PInvoke.RECT(); while (!exitThread) { Thread.Sleep(100); IsParentWindowVisible = PInvoke.IsWindowVisible(ParentWindowHandle) != 0; if (!IsParentWindowVisible) { if (Window.IsVisible) { Window.HideWindow(); } continue; } if (!Window.IsVisible) { Window.ShowWindow(); } PInvoke.GetRealWindowRect(ParentWindowHandle, out bounds); int x = bounds.Left; int y = bounds.Top; int width = bounds.Right - x; int height = bounds.Bottom - y; if (Window.X == x && Window.Y == y && Window.Width == width && Window.Height == height) { continue; } Window.SetWindowBounds(x, y, width, height); Graphics.Resize(width, height); } }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> A RECT extension method that gets a width. </summary> /// /// <param name="r"> The r to act on. </param> /// /// <returns> The width. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static int GetWidth(this PInvoke.RECT r) { return(r.right - r.left); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> A RECT extension method that gets a height. </summary> /// /// <param name="r"> The r to act on. </param> /// /// <returns> The height. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static int GetHeight(this PInvoke.RECT r) { return(r.bottom - r.top); }
public void UpdateTrackingRegion(PInvoke.RECT rect) { _trackingRect = rect; }
public virtual IntPtr WindowProcedureHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Determine which Monitor is displaying the Window var monitor = PInvoke.User32.MonitorFromWindow(hwnd, PInvoke.User32.MonitorOptions.MONITOR_DEFAULTTONEAREST); // Switch on the message. switch ((PInvoke.User32.WindowMessage)msg) { case PInvoke.User32.WindowMessage.WM_DPICHANGED: // Marshal the value in the lParam into a Rect. var newDisplayRect = (PInvoke.RECT)Marshal.PtrToStructure(lParam, typeof(PInvoke.RECT)); // Set the Window's position & size. var upperLeft = new Vector(newDisplayRect.left, newDisplayRect.top); var ul = this.source.CompositionTarget.TransformFromDevice.Transform(upperLeft); var size = new Vector(newDisplayRect.right = newDisplayRect.left, newDisplayRect.bottom - newDisplayRect.top); var hw = this.source.CompositionTarget.TransformFromDevice.Transform(size); this.Left = ul.X; this.Top = ul.Y; this.Width = hw.X; this.Height = hw.Y; // Remember the current DPI settings. var oldDpi = this.CurrentDpi; // Get the new DPI settings from wParam this.CurrentDpi = new Dpi { X = wParam.ToInt32() >> 16, Y = wParam.ToInt32() & 0x0000FFFF }; if (oldDpi.X != this.CurrentDpi.X || oldDpi.Y != this.CurrentDpi.Y) { this.OnDPIChanged(); } handled = true; return(IntPtr.Zero); case PInvoke.User32.WindowMessage.WM_GETMINMAXINFO: // lParam has a pointer to the MINMAXINFO structure. Marshal it into managed memory. var mmi = (PInvoke.User32.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(PInvoke.User32.MINMAXINFO)); if (monitor != IntPtr.Zero) { var monitorInfo = IntPtr.Zero; var rcWorkArea = new PInvoke.RECT(); var rcMonitorArea = new PInvoke.RECT(); try { monitorInfo = Marshal.AllocHGlobal(Marshal.SizeOf <PInvoke.User32.MONITORINFO>()); if (PInvoke.User32.GetMonitorInfo(monitor, monitorInfo)) { var temp = Marshal.PtrToStructure <PInvoke.User32.MONITORINFO>(monitorInfo); // Get the Monitor's working area rcWorkArea = temp.rcWork; rcMonitorArea = temp.rcMonitor; } } finally { if (monitorInfo != IntPtr.Zero) { Marshal.FreeHGlobal(monitorInfo); } } // Adjust the maximized size and position to fit the work area of the current monitor mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left); mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top); mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left); mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top); } // Copy our changes to the mmi object back to the original Marshal.StructureToPtr(mmi, lParam, true); handled = false; return(IntPtr.Zero); default: // Let the WPF code handle all other messages. Return 0. return(IntPtr.Zero); } }