Window Location, Multiple Monitor
-
I'm writing a plug-in in VB.NET to a an application called DesktopSidebar for Windows. Right clicking my plug-in's "panel" shows among other things a Properties Dialog. All I need to do is pop up the dialog in the proper place and on the same monitor in a multiple monitor environment. My plug-in doesn't appear at this point to have the Window handle of the calling application. After quite a bit of trial and error I came up with this: Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Int32, ByRef lpRect As RECT) As Int32 Public Structure RECT Dim Left As Int32 Dim Top As Int32 Dim Right As Int32 Dim Bottom As Int32 End Structure Dim colProcess() As System.Diagnostics.Process Dim oProcess As Diagnostics.Process colProcess = Diagnostics.Process.GetProcessesByName("sidebar") If colProcess.Length > 0 Then oProcess = colProcess(0) Dim handle As System.IntPtr = oProcess.MainWindowHandle Dim loc As RECT If GetWindowRect(handle.ToInt32, loc) > 0 Then 'Where are we docked? If loc.Left = 0 Then Me.Location = New Drawing.Point(loc.Right,(loc.Bottom - loc.Top)/2) Else Me.Location = New Drawing.Point(loc.Left - Me.Width, (loc.Bottom - loc.Top)/2) End If End If End If This works great as long as you have a single monitor, whether DesktopSideBar is docked to the Left or the Right. However, I run in a multi-monitor environment. In my particular setup my primary display is my laptop and my secondary display actually hovers ABOVE my primary display (it's a 19 inch monitor) and I have my desktop extended onto it. The code above, only targets the primary display, even though I dock DesktopSideBar on my secondary monitor. My screen coordinates are still good... just not on the right monitor. Could anyone offer up some suggestions on how to not only determine the monitor my panel is on, but also how to set the location of my dialog to a specific place on the proper monitor?
-
I'm writing a plug-in in VB.NET to a an application called DesktopSidebar for Windows. Right clicking my plug-in's "panel" shows among other things a Properties Dialog. All I need to do is pop up the dialog in the proper place and on the same monitor in a multiple monitor environment. My plug-in doesn't appear at this point to have the Window handle of the calling application. After quite a bit of trial and error I came up with this: Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Int32, ByRef lpRect As RECT) As Int32 Public Structure RECT Dim Left As Int32 Dim Top As Int32 Dim Right As Int32 Dim Bottom As Int32 End Structure Dim colProcess() As System.Diagnostics.Process Dim oProcess As Diagnostics.Process colProcess = Diagnostics.Process.GetProcessesByName("sidebar") If colProcess.Length > 0 Then oProcess = colProcess(0) Dim handle As System.IntPtr = oProcess.MainWindowHandle Dim loc As RECT If GetWindowRect(handle.ToInt32, loc) > 0 Then 'Where are we docked? If loc.Left = 0 Then Me.Location = New Drawing.Point(loc.Right,(loc.Bottom - loc.Top)/2) Else Me.Location = New Drawing.Point(loc.Left - Me.Width, (loc.Bottom - loc.Top)/2) End If End If End If This works great as long as you have a single monitor, whether DesktopSideBar is docked to the Left or the Right. However, I run in a multi-monitor environment. In my particular setup my primary display is my laptop and my secondary display actually hovers ABOVE my primary display (it's a 19 inch monitor) and I have my desktop extended onto it. The code above, only targets the primary display, even though I dock DesktopSideBar on my secondary monitor. My screen coordinates are still good... just not on the right monitor. Could anyone offer up some suggestions on how to not only determine the monitor my panel is on, but also how to set the location of my dialog to a specific place on the proper monitor?
When running an application on a multiple monitor environment the coordinates of the form are the coordinates for the combined desktop. The .NET Framework provides the Form.DesktopLocation property to set and get the location of forms in multimonitor environments. So, in the code you posted above you should remove stataments like Me.Location = New Drawing.Point(loc.Right,(loc.Bottom - loc.Top)/2) replacing them with
Me.DesktopLocation = New System.Drawing.Point(loc.Right,(loc.Bottom - loc.Top)/2)