0

I have looked at every similar question and none work for me. I am programming a winForm that will show me the screen factor. I have tryed reading from Registry (doesnt work) i have tryed using DLL "gdi32.dll" , that works but only on Win10 not on Win7. I Have also tryed :

float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
    dpiX = graphics.DpiX;
    dpiY = graphics.DpiY;
}

but it doesnt work also. I know that this question is repeated but all the answers that were in the previous questions didnt help, so thats why i am opening a new one. So, can someone help me please?

  • Possible duplicate of [How to get Windows Display settings?](https://stackoverflow.com/questions/5977445/how-to-get-windows-display-settings) – SᴇM Jul 12 '19 at 08:40
  • Why do you need this information? Is it just for diagnostics, or are you trying to re-implement scaling in your app? – Neil Jul 12 '19 at 08:46
  • @SᴇM I wrote in my comment that this question is repeated. so i know it is a duplicate, but in the original question i didnt get the right answer. There are a lot of answers but non of them work. SO thats why i opened another one. – Nemanja Vidačković Jul 12 '19 at 08:46
  • @Neil i am running a WinForm in Labview. Labview has a button that is covered by the WinForm (like a Guide) and i have fixed coordinates where the button is. IF the user uses a different scaling factor (125%,...) the button has different coordinates so. – Nemanja Vidačković Jul 12 '19 at 08:50
  • 1
    @NemanjaVidačković What you mean under _"it doesn't work"_? – SᴇM Jul 12 '19 at 08:53
  • @SᴇM The result from dpiX and dpiY is always 96 . when i scale the screen to 125% or 150% i also get 96 so thats not realy helpful – Nemanja Vidačković Jul 12 '19 at 10:38

1 Answers1

0

You can use GetDpiForWindow with a Manifest.

uint nDPI = GetDpiForWindow(this.Handle);

I get =>

        100% : 96
        125% : 120
        150% : 144
        175% : 168

Declaration =>

    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern uint GetDpiForWindow(IntPtr hwnd);

Manifest =>

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
     </windowsSettings>
  </application>
Castorix
  • 1,465
  • 1
  • 9
  • 11