admin管理员组文章数量:1025207
In the project I contribute to (C#, WPF) I need to position multiple OSD windows on multiple screens, e.g. top-left, center,bottom-right, etc.
I use the following code to get all screens' workarea and dpi.
public readonly struct ScreenInfo(Rect workArea, uint dpiX, uint dpiY, bool isPrimary)
{
public Rect WorkArea { get; } = workArea;
public uint DpiX { get; } = dpiX;
public uint DpiY { get; } = dpiY;
public bool IsPrimary { get; } = isPrimary;
}
public static void UpdateScreenInfos()
{
Screens.Clear();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnumProc, IntPtr.Zero);
}
[DllImport("user32.dll")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumDisplayMonitorsDelegate lpfnEnum, IntPtr dwData);
private delegate bool EnumDisplayMonitorsDelegate(HMONITOR hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
private static bool MonitorEnumProc(HMONITOR hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
{
MONITORINFO monitorInfo = new() { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
if (!PInvoke.GetMonitorInfo(hMonitor, ref monitorInfo))
return true;
if (!PInvoke.GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY).Succeeded)
return true;
var workArea = monitorInfo.rcWork;
var multiplierX = 96d / dpiX;
var multiplierY = 96d / dpiY;
Screens.Add(new ScreenInfo(
new Rect(workArea.X * multiplierX, workArea.Y * multiplierY, workArea.Width * multiplierX, workArea.Height * multiplierY),
dpiX, dpiY,
(monitorInfo.dwFlags & PInvoke.MONITORINFOF_PRIMARY) != 0
));
return true;
}
The codes above work pretty fine when zoom ratios of all screens are the same. But if I have for example two screens: one 2K resolution with 150% zoom ratio, the other 4K with 125%, workarea calculated by MonitorEnumProc
has an offset on all screens except the primary one.
My question is, is there a safe and reliable solution to convert those coord in native DPI into logic DPI in WPF? It should work under multi-screen environment, also under multi-zoom-ratios.
In the project I contribute to (C#, WPF) I need to position multiple OSD windows on multiple screens, e.g. top-left, center,bottom-right, etc.
I use the following code to get all screens' workarea and dpi.
public readonly struct ScreenInfo(Rect workArea, uint dpiX, uint dpiY, bool isPrimary)
{
public Rect WorkArea { get; } = workArea;
public uint DpiX { get; } = dpiX;
public uint DpiY { get; } = dpiY;
public bool IsPrimary { get; } = isPrimary;
}
public static void UpdateScreenInfos()
{
Screens.Clear();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnumProc, IntPtr.Zero);
}
[DllImport("user32.dll")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumDisplayMonitorsDelegate lpfnEnum, IntPtr dwData);
private delegate bool EnumDisplayMonitorsDelegate(HMONITOR hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
private static bool MonitorEnumProc(HMONITOR hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
{
MONITORINFO monitorInfo = new() { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
if (!PInvoke.GetMonitorInfo(hMonitor, ref monitorInfo))
return true;
if (!PInvoke.GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY).Succeeded)
return true;
var workArea = monitorInfo.rcWork;
var multiplierX = 96d / dpiX;
var multiplierY = 96d / dpiY;
Screens.Add(new ScreenInfo(
new Rect(workArea.X * multiplierX, workArea.Y * multiplierY, workArea.Width * multiplierX, workArea.Height * multiplierY),
dpiX, dpiY,
(monitorInfo.dwFlags & PInvoke.MONITORINFOF_PRIMARY) != 0
));
return true;
}
The codes above work pretty fine when zoom ratios of all screens are the same. But if I have for example two screens: one 2K resolution with 150% zoom ratio, the other 4K with 125%, workarea calculated by MonitorEnumProc
has an offset on all screens except the primary one.
My question is, is there a safe and reliable solution to convert those coord in native DPI into logic DPI in WPF? It should work under multi-screen environment, also under multi-zoom-ratios.
本文标签: cHow to convert a coord in native DPI to one in WPF logic DPIStack Overflow
版权声明:本文标题:c# - How to convert a coord in native DPI to one in WPF logic DPI? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619788a2159506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论