I have 2 monitors:
1st monitor: 1920 x 1080, 100 %, 96 dpi, primary monitor
2nd monitor: 1280 x 1024, 150 %, 144 dpi, right of primary monitor
I want to open the MainWindow 1 pixel right of the left edge of the secondary monitor. All I tried wasn't successful.
Either the window opens on the right side of the primary monitor or it overlaps the right edge of the secondary monitor.
// AssemblyInfo.cs
[assembly: DisableDpiAwareness]
// C++
BOOL NativeHelpers::PerMonitorDPIHelper::SetPerMonitorDPIAware()
{
auto result = SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
return S_OK == result ? TRUE : FALSE;
}
double NativeHelpers::PerMonitorDPIHelper::GetDpiForPoint(LONG x, LONG y)
{
POINT pt;
pt.x = x;
pt.y = y;
auto monitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
UINT newDpiX;
UINT newDpiY;
if (FAILED(GetDpiForMonitor(monitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI, &newDpiX, &newDpiY)))
{
newDpiX = 96;
newDpiY = 96;
}
return ((double) newDpiX);
}
// C#
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
PerMonitorDPIHelper.SetPerMonitorDPIAware();
}
}
public partial class MainWindow : Window
{
private WF.Screen PrimaryScreen;
public MainWindow()
{
PrimaryScreen = WF.Screen.FromPoint(new System.Drawing.Point(1, 1));
WF.Screen dest = WF.Screen.FromPoint(new System.Drawing.Point(PrimaryScreen.Bounds.Right, 0));
double dpi = PerMonitorDPIHelper.GetDpiForPoint(dest.Bounds.Left, dest.Bounds.Top),
scale = dpi / 96d;
Left = PrimaryScreen.Bounds.Right + 1;
Top = 100;
Debug.WriteLine("MainWindow Left: " + Left);
InitializeComponent();
Title = "" + dpi + " dpi";
}
}