public static IList <Display> GetDisplays() { var list = new List <Display>(); int index = 1; NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr monitor, IntPtr _, ref NativeRect __, IntPtr ___) => { var monitorInfoEx = MonitorInfoEx.Create(); if (!NativeMethods.GetMonitorInfo(monitor, ref monitorInfoEx)) { return(true); } NativeDpiMethods.GetDpiForMonitor(monitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY); var display = new Display { Handle = monitor, Index = index++, Bounds = (Rectangle)monitorInfoEx.Monitor, WorkingArea = (Rectangle)monitorInfoEx.WorkArea, IsPrimary = (monitorInfoEx.Flags & MonitorInfoFlags.Primary) == MonitorInfoFlags.Primary, DeviceName = monitorInfoEx.DeviceName, Dpi = new Point((int)dpiX, (int)dpiY), }; list.Add(display); return(true); }, IntPtr.Zero);
/// <summary> /// Attach a DpiHandler to the specified window /// </summary> /// <param name="window">Windows</param> /// <param name="dpiHandler">DpiHandler</param> private static void AttachDpiHandler(Window window, DpiHandler dpiHandler) { if (Log.IsVerboseEnabled()) { Log.Verbose().WriteLine("Registering the UpdateLayoutTransform subscription for {0}", window.GetType()); } // Add the layout transform action var transformSubscription = dpiHandler.OnDpiChanged.Subscribe(dpiChangeInfo => window.UpdateLayoutTransform((double)dpiChangeInfo.NewDpi / DpiHandler.DefaultScreenDpi)); window.WinProcMessages().Subscribe(message => { dpiHandler.HandleWindowMessages(message); switch (message.Message) { case WindowsMessages.WM_NCCREATE: // Apply scaling 1x time window.UpdateLayoutTransform((double)NativeDpiMethods.GetDpi(message.Handle) / DpiHandler.DefaultScreenDpi); break; case WindowsMessages.WM_DESTROY: // Remove layout transform if (Log.IsVerboseEnabled()) { Log.Verbose().WriteLine("Removing the UpdateLayoutTransform subscription for {0}", window.GetType()); } transformSubscription.Dispose(); break; } }); }
public Window GetWindow(IntPtr handle) { var interopWindow = InteropWindowFactory.CreateFor(handle); interopWindow.Fill(InteropWindowCacheFlags.Info | InteropWindowCacheFlags.Text); var windowInfo = interopWindow.Info.Value; Dwm.GetExtendedFrameBounds(handle, out var nativeBounds); var innerBounds = (Rectangle)nativeBounds; var outerBounds = (Rectangle)windowInfo.Bounds; Rectangle bounds; Thickness border; if (outerBounds.Width > 0 || outerBounds.Height > 0) { var rawBorder = innerBounds.GetBorder(outerBounds); if (rawBorder.Left < 0 || rawBorder.Top < 0 || rawBorder.Right < 0 || rawBorder.Right < 0) { bounds = new Rectangle( innerBounds.Left - Math.Min(rawBorder.Left, 0), innerBounds.Top - Math.Min(rawBorder.Top, 0), innerBounds.Width - Math.Min(rawBorder.Left, 0) - Math.Min(rawBorder.Right, 0), innerBounds.Height - Math.Min(rawBorder.Top, 0) - Math.Min(rawBorder.Bottom, 0)); border = new Thickness( Math.Max(rawBorder.Left, 0), Math.Max(rawBorder.Top, 0), Math.Max(rawBorder.Right, 0), Math.Max(rawBorder.Bottom, 0)); } else { bounds = innerBounds; border = rawBorder; } } else { bounds = innerBounds; border = Thickness.Empty; } var dpi = (int)NativeDpiMethods.GetDpiForWindow(handle); return(new Window { Handle = handle, Title = interopWindow.Text, Bounds = bounds, Border = border, Dpi = new Point(dpi, dpi), }); }
public void TestScreenbounds() { var screenboundsAllScreens = GetScreenBoundsAllScreens(); var screenboundsDisplayInfo = DisplayInfo.ScreenBounds; // The following scales the screenboundsAllScreens which comes from build in code without DPI awareness // with the current DPI so it should also work when running with a different DPI setting var monitorHandle = DisplayInfo.AllDisplayInfos.First().MonitorHandle; NativeDpiMethods.GetDpiForMonitor(monitorHandle, Dpi.Enums.MonitorDpiType.EffectiveDpi, out var xDpi, out var yDpi); if (xDpi != DpiHandler.DefaultScreenDpi) { var newSize = DpiHandler.ScaleWithDpi(screenboundsAllScreens.Size, xDpi); screenboundsAllScreens = new NativeRect(screenboundsAllScreens.Location, newSize); } Assert.Equal(screenboundsAllScreens, screenboundsDisplayInfo); }
/// <summary> /// This takes the height and width of the Popup and will return the location where it should be displayed /// </summary> /// <param name="actualPopupWidth">double</param> /// <param name="actualPopupHeight">double</param> /// <returns>Point</returns> public Point GetPosition(double actualPopupWidth, double actualPopupHeight) { var taskbar = Shell32Api.TaskbarPosition; var taskbarBounds = taskbar.Bounds; var actualSize = new NativeSize((int)actualPopupWidth, (int)actualPopupHeight); // Use the DPI of the desktop var dpi = NativeDpiMethods.GetDpi(InteropWindowQuery.GetDesktopWindow().Handle); actualSize = DpiHandler.ScaleWithDpi(actualSize, dpi); int x, y; // Define the new position switch (taskbar.AppBarEdge) { case AppBarEdges.Left: x = taskbarBounds.Right + _xOffset; y = taskbarBounds.Bottom - _yOffset - actualSize.Height; break; case AppBarEdges.Top: x = taskbarBounds.Right - _xOffset - actualSize.Width; y = taskbarBounds.Bottom + _yOffset; break; case AppBarEdges.Right: x = taskbarBounds.Left - _xOffset - actualSize.Width; y = taskbarBounds.Bottom - _yOffset - actualSize.Height; break; case AppBarEdges.Bottom: default: x = taskbarBounds.Right - _xOffset - actualSize.Width; y = taskbarBounds.Top - _yOffset - actualSize.Height; break; } var position = DpiHandler.UnscaleWithDpi(new NativePoint(x, y), dpi); Log.Debug().WriteLine("Taskbar location {0} at {1}, calculate popup position: {2}", taskbar.AppBarEdge, taskbarBounds, position); return(position); }
protected override void OnStartup(StartupEventArgs e) { LogSettings.RegisterDefaultLogger <DebugLogger>(LogLevels.Verbose); NativeDpiMethods.EnableDpiAware(); base.OnStartup(e); }
/// <inheritdoc /> protected override void CreateHandle() { _dpiAwarenessContextScope = NativeDpiMethods.ScopedThreadDpiAwarenessContext(DpiAwarenessContext.PerMonitorAwareV2, DpiAwarenessContext.PerMonitorAware); base.CreateHandle(); }
protected AbstractAdorner(IDrawableContainer owner) { _size.Width = _size.Height = DpiHandler.ScaleWithDpi(5, NativeDpiMethods.GetDpi(InteropWindowQuery.GetDesktopWindow().Handle)); Owner = owner; }