Results 1 to 6 of 6

Thread: Does Screen class work with dual monitors?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    United States
    Posts
    7

    Question Does Screen class work with dual monitors?

    I am creating a function in a core DLL that will compute the size a form object should be based on the screen resolution and required border, taking into consideration any taskbars, docked windows, etc...

    I am using the System.Windows.Forms.Screen namespace.

    My question is, will this name space only work on single monitor machines? If someone is using the application on a machine with dual monitors will it base the calculations using the resolution of the primary video card, or which ever screen the application is being displayed on.

    Its confusing me because its pulling the information from the primary screen. So I'm assuming this function will not work on a machine running dual monitors. Unless all monitors are running the same resolution.

    I would just test it, however I don't have access to a second monitor. I have included the code incase someone has a dual monitor machine they can run this on and see what happens.

    Thanks Very Much!

    Required Namespaces:
    System.Windows.Forms
    System.Drawing


    Code:
    /// <summary>
    /// A basic function that decides where the form should be located on screen
    /// based on the required border and the screen resolution. This is usually
    /// used for main forms, such as the MDI object.
    /// </summary>
    /// <param name="verticalBorder">
    /// The required vertical border (space between the edge of the screen and the form).
    /// </param>
    /// <param name="horizontalBorder">
    /// The required horizontal border (space between the edge of the screen and the form).
    /// </param>
    /// <returns>
    /// Returns the exact size of the form object, including the calculated borders.
    /// </returns>
    public static Size SetWindowSize(int verticalBorder, int horizontalBorder)
    	{
    		// Create the fields to store the border information.
    		int theHeight, theWidth;
    
    		// Compute the exact height and width of the form.
    		theHeight	= Screen.PrimaryScreen.WorkingArea.Height	- verticalBorder;
    		theWidth	= Screen.PrimaryScreen.WorkingArea.Width	- horizontalBorder;
    
    		// Return the new size to the calling function.
    		return new Size(theWidth, theHeight);
    	}
    Code:
    // Get the size for the form.
    this.Size = common.SetWindowSize(10, 10);
    this.CenterToScreen();
    - Billiard
    If you would like me to read your code, please use [code] tags.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Does Screen class work with dual monitors?

    It's not a namespace, it's a class. The PrimaryScreen property is, as the name suggests, a Screen object that represents the primary monitor. Given that the vast majority of users will only ever have one monitor it is quite logical that there would be a property specific to it. I suggest you read the help topics on the Screen class and its members. They have all the information you need. The help/MSDN really should be the first place you look for information like this, then look elsewhere if it doesn't have what you need or you can't understand what it says.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    United States
    Posts
    7

    Re: Does Screen class work with dual monitors?

    Quote Originally Posted by jmcilhinney
    It's not a namespace, it's a class. The PrimaryScreen property is, as the name suggests, a Screen object that represents the primary monitor. Given that the vast majority of users will only ever have one monitor it is quite logical that there would be a property specific to it. I suggest you read the help topics on the Screen class and its members. They have all the information you need. The help/MSDN really should be the first place you look for information like this, then look elsewhere if it doesn't have what you need or you can't understand what it says.
    excuuuuuseeeeeeeeeeee ME could you please email my head back to me?
    - Billiard
    If you would like me to read your code, please use [code] tags.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Does Screen class work with dual monitors?

    Quote Originally Posted by Billiard
    excuuuuuseeeeeeeeeeee ME could you please email my head back to me?
    I'm not sure whether you're having a go at yourself or at me.

    The Screen class has a property named AllScreens that will give you an array of Screen objects. It also has methods to return the specific Screen object that contains the majority of a control, rectangle, object by handle or that contains a specific point. Once you have a Screen object you can then use its WorkingArea property as you have been. As an example, this:
    VB Code:
    1. Dim myScreen As Screen = Screen.PrimaryScreen
    would have the same result as this:
    VB Code:
    1. Dim myScreen As Screen
    2.  
    3. For Each myScreen In Screen.AllScreens
    4.     If myScreen.Primary Then
    5.         Exit For
    6.     End If
    7. Next myScreen
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Does Screen class work with dual monitors?

    Or even
    Code:
    Screen myScreen = Screen.PrimaryScreen;
    Code:
    foreach (Screen myScreen in Screen.AllScreens)
    {
        if (myScreen.Primary)
            break;
    }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Does Screen class work with dual monitors?

    Possibly, but only for those who know where they are.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width