Resolved Best way to get second and third monitor screen resolution?

dv2020

Active member
Joined
Dec 18, 2020
Messages
30
Programming Experience
1-3
Hi All,

Have been trying to work out the best way to obtain the active screen resolution for my application, as I am trying to resize some fonts and buttons if the user moves the application over to there 2nd or 3rd monitor.

Currently using the code below when seems to only obtain the screen size of the main monitor.
C#:
  screenWidth = Screen.PrimaryScreen.Bounds.Width;
  screenHeight = Screen.PrimaryScreen.Bounds.Height;

Does anyone have a good, clean way of obtaining the sizes of the active monitor that the user is operating it, e.g where there mouse is currently active?

Thanks in advance,

David
 
Last edited by a moderator:
Solution
This checks which screen form Location is in on LocationChanged event. Note that a form can be moved outside any screen and that activeScreen can be null.
C#:
private void Form4_LocationChanged(object sender, EventArgs e)
{
    var activeScreen = Screen.AllScreens.Where(sc => sc.Bounds.Contains(Location)).FirstOrDefault();
    Console.WriteLine(activeScreen?.DeviceName);
}
This checks which screen form Location is in on LocationChanged event. Note that a form can be moved outside any screen and that activeScreen can be null.
C#:
private void Form4_LocationChanged(object sender, EventArgs e)
{
    var activeScreen = Screen.AllScreens.Where(sc => sc.Bounds.Contains(Location)).FirstOrDefault();
    Console.WriteLine(activeScreen?.DeviceName);
}
 
Solution
This checks which screen form Location is in on LocationChanged event. Note that a form can be moved outside any screen and that activeScreen can be null.
C#:
private void Form4_LocationChanged(object sender, EventArgs e)
{
    var activeScreen = Screen.AllScreens.Where(sc => sc.Bounds.Contains(Location)).FirstOrDefault();
    Console.WriteLine(activeScreen?.DeviceName);
}
Thanks John,

That worked perfectly.

Kind Regards

David
 
Back
Top Bottom