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.
Subarta RayPosted Dec 26, 2023, 5:54 AM
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...
}
}
Anandu G NathPosted Dec 21, 2023, 5:23 AM
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.Jayraj ChhayaPosted Dec 8, 2023, 9:52 AM
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:
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.
Sam HobbsPosted Dec 7, 2023, 10:08 PM
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.Satyaprakash SamantarayPosted Dec 7, 2023, 9:22 AM
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.Vijay Pratap SinghPosted Dec 7, 2023, 4:29 AM
Hopefully, this code will help