How can I retrieve the screen resolution that my C# Winform App is running on?
h> Display* disp = XOpenDisplay(NULL); Screen* scrn = DefaultScreenOfDisplay(disp); int height = scrn->height; int width = scrn->width; with the linker option -lX11 .
Right-click on the desktop of your computer and select "Screen resolution". Click the drop-down menu labeled "Resolution" and use the slider to select the desired screen resolution. Click "Apply".
Do you need just the area a standard application would use, i.e. excluding the Windows taskbar and docked windows? If so, use the Screen.WorkingArea property. Otherwise, use Screen.Bounds.
If there are multiple monitors, you need to grab the screen from your form, i.e.
Form myForm; Screen myScreen = Screen.FromControl(myForm); Rectangle area = myScreen.WorkingArea;
If you want to know which is the primary display screen, use the Screen.Primary property. Also, you can get a list of screens from the Screen.AllScreens property.
The given answer is correct, as far as it goes. However, when you have set your Text size to anything more than 125%, Windows (and .NET) start to fib about the size of the screen in order to do auto-scaling for you.
Most of the time, this is not an issue - you generally want Windows and .NET to do this. However, in the case where you really, truly need to know the actual count of pixels on the screen (say, you want to paint directly to the desktop DC), you can do the following. I've only tried this on Win10. YMMV on other Windows versions.
So far, this is the only way I've found to get true screen pixel count if you don't want to globally turn off DPI awareness in your app. Note that this example gets the Primary display size - you will need to modify this to get other screens.
[DllImport("User32.dll")] static extern IntPtr GetDC(IntPtr hwnd); [DllImport("User32.dll")] static extern int ReleaseDC(IntPtr hwnd, IntPtr dc); [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); IntPtr primary = GetDC(IntPtr.Zero); int DESKTOPVERTRES = 117; int DESKTOPHORZRES = 118; int actualPixelsX = GetDeviceCaps(primary, DESKTOPHORZRES); int actualPixelsY = GetDeviceCaps(primary, DESKTOPVERTRES); ReleaseDC(IntPtr.Zero, primary);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With