I need to find out the screen size of any computer that my application is running on. I want to center my forms without using the whole screen. I am using Visual Studio to develop this application. I have looked at MS docs but there are now examples of how or where to execute the comomand. I need both Horizontal and Vertical numbers.
Any help would be greatly appreciated.

Vijay Pratap SinghRank #381 · 6.6K reputationPosted Dec 6, 2023, 11:29 PM EST
Hopefully, this code will help
using System; using System.Windows.Forms; namespace ScreenSizeExample { public partial class MainForm : Form { public MainForm() { InitializeComponent(); // Call the method to center the form CenterFormOnScreen(); } private void CenterFormOnScreen() { // Get the primary screen Screen primaryScreen = Screen.PrimaryScreen; // Get the screen width and height int screenWidth = primaryScreen.WorkingArea.Width; int screenHeight = primaryScreen.WorkingArea.Height; // Calculate the center position int centerX = (screenWidth - Width) / 2; int centerY = (screenHeight - Height) / 2; // Set the form's location to be centered Location = new System.Drawing.Point(centerX, centerY); } // Other methods and event handlers for your form... } }Satyaprakash SamantarayRank #101 · 21.1K reputationPosted Dec 7, 2023, 4:22 AM EST
For Horizontal and Vertical numbers.
multi-screen setup and want to get the combined size of all the screens, we can also do this with the
SystemParametersclass in C#. TheSystemParameters.VirtualScreenHeightproperty gets the full height of all the monitors. TheSystemParameters.VirtualScreenWidthproperty gets the width of all the monitors. You can use both these properties to get the combined size of all the monitors.using System.Windows; double height = SystemParameters.VirtualScreenHeight; double width = SystemParameters.VirtualScreenWidth; double resolution = height * widthSam HobbsRank #107 · 20.5K reputationPosted Dec 7, 2023, 5:08 PM EST
There might be an easier way. You do not say whether you are using Windows Forms but the WPF and other frameworks will have features equivalent to the following.
A window has a
StartPositionproperty.Form.StartPosition Property
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.startposition?view=windowsdesktop-8.0
One of the possible values of that is
CenterScreen.Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 8, 2023, 4:52 AM EST
To determine the screen size of a computer using C#, you can utilize the
System.Windows.Forms.Screenclass. This class provides properties that allow you to retrieve information about the screen, including its size.To get the screen size, you can use the
Screen.PrimaryScreen.Boundsproperty, which returns anRectangleobject representing the dimensions of the primary screen. TheWidth,Heightproperties of theRectangleobject will give you the horizontal and vertical measurements, respectively.Here's an example of how you can retrieve the screen size:
using System; using System.Windows.Forms; class Program { static void Main() { Screen primaryScreen = Screen.PrimaryScreen; int screenWidth = primaryScreen.Bounds.Width; int screenHeight = primaryScreen.Bounds.Height; Console.WriteLine("Screen Width: " + screenWidth); Console.WriteLine("Screen Height: " + screenHeight); } }In the above example, we first obtain the
Screenobject representing the primary screen using theScreen.PrimaryScreenproperty. We then access theBoundsproperty of theScreenobject to retrieve the dimensions of the screen. Finally, we extract the width and height values from theBoundsproperty and display them usingConsole.WriteLine().By using this approach, you can easily determine the screen size of the computer running your application and use the obtained measurements to center your forms accordingly.
Anandu G NathRank #681 · 3.5K reputationPosted Dec 21, 2023, 12:23 AM EST
In C#, you can determine the screen size of a computer using the
System.Windows.Forms.Screenclass. This class provides information about the display devices connected to the system. You can retrieve the screen size in pixels, working area, and other related information.static void Main() { // Get the primary screen's bounds (total size) Screen primaryScreen = Screen.PrimaryScreen; int screenWidth = primaryScreen.Bounds.Width; int screenHeight = primaryScreen.Bounds.Height; Console.WriteLine($"Screen Width: {screenWidth}px"); Console.WriteLine($"Screen Height: {screenHeight}px"); }Subarta RayRank #336 · 7.4K reputationPosted Dec 26, 2023, 12:54 AM EST
Hi Edward ,
using System;
using System.Windows.Forms;
namespace YourNamespace
{
public partial class YourForm : Form
{
public YourForm()
{
InitializeComponent();
// Center the form on the screen
CenterToScreen();
}
private void CenterToScreen()
{
// Get the screen size
Screen screen = Screen.FromControl(this);
int screenWidth = screen.Bounds.Width;
int screenHeight = screen.Bounds.Height;
// Calculate the position to center the form
int x = (screenWidth - Width) / 2;
int y = (screenHeight - Height) / 2;
// Set the form position
Location = new System.Drawing.Point(x, y);
}
// Rest of your form code...
}
}