How to Retrieve the Screen Resolution from a C# Winform App

How to retrieve the Screen Resolution from a C# winform app?

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.

Get screen size in pixels in windows form in C#

Check the Screen class and its property Bounds

Screen.PrimaryScreen.Bounds.Width;
Screen.PrimaryScreen.Bounds.Height;
Screen.PrimaryScreen.Bounds.Size;

How to fit Windows Form to any screen resolution?

Can't you start maximized?

Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized

How can I get the active screen dimensions?

Screen.FromControl, Screen.FromPoint and Screen.FromRectangle should help you with this. For example in WinForms it would be:

class MyForm : Form
{
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
}

I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.

static class ExtensionsForWPF
{
public static System.Windows.Forms.Screen GetScreen(this Window window)
{
return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
}

How to autosize the form window based on screen resolution during run time (Where the size of the window created during design is too large)

Setting the WindowState = Maximized will cause the window to open to the full extents of the screen resolution. It doesn't matter what that value is, it will match it.

Sample Image

Edit:
From your comments, it sounds like you want what the AutoSize property will accomplish. I updated the form to add some controls and set the AutoSize = True and the AutoSizeMode = GrowAndShrink. Between these three properties, you should be able to get the form to do exactly what you wish. The one thing to pay attention to is the full extents of your controls within the form. From this picture you can see the form during runtime will resize to fit both text boxes while in the editor, I shrunk the form to hide almost everything. Please also note that in the example below, I set the WindowState = Normal.

Sample Image



Related Topics



Leave a reply



Submit