Results 1 to 4 of 4

Thread: Screen.GetWorkingArea and PointToScreen problem with dual screen

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Screen.GetWorkingArea and PointToScreen problem with dual screen

    Hi,

    I have a button btnRegEx that when pressed shows a ContextMenuStrip, similar to the '>' button in the Find/Replace dialog in Visual Studio when you are using Regular Expressions.

    I have a ContextMenuStrip cms which I show using its Show(x,y) method.

    At first I was simply setting x and y (the location of the ContextMenuStrip) to some point next to the button, but I noticed that when the button is close to the edge of the screen it goes off screen. I wanted to prevent that so I built some logic into the x-y calculation.
    When the ContextMenuStrip width is larger than the 'remaining space left' I simply show it more to the left. Same for its Height of course.

    I am using the Screen.GetWorkingArea(point) method to determine the 'remaining space left'. As I understand it, it returns the working area closest to the point you specify (to which I pass the button's location).

    As long as I stay on my first monitor, it is all working perfectly fine. The problem occurs when I move the form to my second monitor. The contextmenustrip is still showing on the first monitor for some reason...

    Here is the code I am using:
    vb.net Code:
    1. Private Sub btnRegEx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegEx.Click
    2.         Dim p As Point = PointToScreen(New Point(btnRegEx.Left, btnRegEx.Top))
    3.         Dim x, y As Integer
    4.  
    5.         Dim openWidth As Integer = Screen.GetWorkingArea(p).Width - p.X
    6.         If cms.Width > openWidth Then
    7.             x = Screen.GetWorkingArea(p).Width - cms.Width
    8.         Else
    9.             x = p.X + btnRegEx.Width + 2
    10.         End If
    11.         Dim openHeight As Integer = Screen.GetWorkingArea(p).Height - p.Y
    12.         If cms.Height > openHeight Then
    13.             y = Screen.GetWorkingArea(p).Height - cms.Height
    14.         Else
    15.             y = p.Y + btnRegEx.Height + 2
    16.         End If
    17.  
    18.         cms.Show(x, y)
    19.     End Sub

    I have noticed that when I click the button when the form is on the second monitor, the buttons location (p) is calculated with the first monitor's top-left corner as (0,0), even though it is on the second monitor.
    This way, the GetWorkingArea function returns something like 1280, while the buttons x-location is something like 1700...

    So, how do I get the buttons coordinate relative to the actual screen it is on, rather than the entire dual monitor screens as a whole?

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Screen.GetWorkingArea and PointToScreen problem with dual screen

    How do you have your multi-monitor set up? Extended desktop?

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Screen.GetWorkingArea and PointToScreen problem with dual screen

    I think you could use something like this to find the relative height/width:

    Code:
            Dim p As Point = PointToScreen(Button1.Location)
    
    
            For i = 0 To Screen.AllScreens.Count - 1
                If p.X > Screen.AllScreens(i).Bounds.Width Then
                    p.X -= Screen.AllScreens(i).Bounds.Width
                End If
                If p.Y > Screen.AllScreens(i).Bounds.Height Then
                    p.Y -= Screen.AllScreens(i).Bounds.Height
                End If
            Next

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Screen.GetWorkingArea and PointToScreen problem with dual screen

    Quote Originally Posted by MaximilianMayrhofer View Post
    How do you have your multi-monitor set up? Extended desktop?
    Does that matter? At the moment I have it in extended desktop, but the users of my application might have it in any configuration they like, and obviously the problem should not occur in any of them.

    Quote Originally Posted by Negative0 View Post
    I think you could use something like this to find the relative height/width:

    Code:
            Dim p As Point = PointToScreen(Button1.Location)
    
    
            For i = 0 To Screen.AllScreens.Count - 1
                If p.X > Screen.AllScreens(i).Bounds.Width Then
                    p.X -= Screen.AllScreens(i).Bounds.Width
                End If
                If p.Y > Screen.AllScreens(i).Bounds.Height Then
                    p.Y -= Screen.AllScreens(i).Bounds.Height
                End If
            Next
    I'll try it later, thanks.

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