Skip to content

Commit 4216b78

Browse files
SchreinerKsskodje
authored andcommitted
dpiAwareness pt2 (docking)
1 parent 6c0d84d commit 4216b78

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Demo/Utilities/Win32.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Windows;
4+
using System.Windows.Interop;
35

46
namespace Demo.Utilities
57
{
@@ -27,8 +29,36 @@ public class Win32
2729
internal static extern IntPtr GetTopWindow(IntPtr hWnd);
2830
[DllImport("User32")]
2931
internal static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
30-
32+
[DllImport("user32.dll", SetLastError=true)]
33+
static extern bool GetWindowRect(IntPtr hwnd, out W32Rect lpRect);
34+
[DllImport("shcore.dll",SetLastError = true)]
35+
private static extern uint SetProcessDpiAwareness(int awareness);
3136
#endregion
37+
38+
/// <summary>
39+
/// Gets the window rect in device units.
40+
/// </summary>
41+
/// <param name="window">The window.</param>
42+
/// <returns>Rect.</returns>
43+
public static Rect GetWindowRect(Window window) {
44+
var hwnd = new WindowInteropHelper(window).Handle;
45+
GetWindowRect(hwnd, out var r);
46+
return new Rect(r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top);
47+
}
48+
49+
private const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
50+
/// <summary>
51+
/// Sets the dpi awareness for the current process to: per monitor dpi aware.
52+
/// </summary>
53+
public static void SetProcessDpiAwarenessPerMonitor()
54+
{
55+
var result = SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
56+
switch (result) {
57+
case 0: return;
58+
case 0x80070005: return; // The DPI awareness is already set, either by calling this API previously or through the application (.exe) manifest.
59+
default: Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); return;
60+
}
61+
}
3262
}
3363

3464
[StructLayout(LayoutKind.Sequential)]

Demo/WindowBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private Window FindWindowUnderThisAt(Window source, Point screenPoint) // WPF u
159159
var allWindows = SortWindowsTopToBottom(Application.Current.Windows.OfType<Window>().Where(x => x.GetType().ToString() != "Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerWindow"
160160
&& x.GetType().ToString() != "Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow"));
161161
var windowsUnderCurrent = from win in allWindows
162-
where (win.WindowState == WindowState.Maximized || new Rect(win.Left, win.Top, win.Width, win.Height).Contains(screenPoint))
162+
where (win.WindowState == WindowState.Maximized || Win32.GetWindowRect(win).Contains(screenPoint))
163163
&& !Equals(win, source)
164164
select win;
165165
return windowsUnderCurrent.FirstOrDefault();

0 commit comments

Comments
 (0)