Different dpi: How to open a window at a specific point?

Heiko 1,291 Reputation points
2022-07-10T12:07:29.96+00:00

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";  
     }  
}  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,826 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,305 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Heiko 1,291 Reputation points
    2022-07-12T14:45:15.277+00:00

    No, I'm using no Nuget package. PerMonitorDPIHelper is a simple C++ class, for calling some methods (see code). WF.Screen is the Screen type in namespace WF = System.Windows.Forms.

    But I found a workaround. Instead of using the Left and Top properties of the MainWindow, I always control the window position and size via Win32 functions like...

    SetWindowPos()  
    GetWindowRect()  
    

    ...and it works. Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.