UserForm displayed on second monitor

G

Guest

Hello!

I have a somewhat annoying problem... I work with two monitors, with
Word or Excel on one side and the VBA IDE on the other.

When I display a userform, it shows up on the monitor where the IDE is
located (whether or not the IDE is actually openned) instead of in the
middle of the monitor where Word or Excel is openned.

Is there some settings that would prevent that?

Thanks for your help

J Whales
(anti-spam alias)
 
H

Howard Kaikow

Where a Userform is displayed depends on the handle of the Parent of the
Userform.
I've always seen Userforms associated with the main Word window, but then
I've never used dual monitors.

Perhaps you need to set the Parent of the Userform when using two monitors?
 
R

RB Smissaert

Have just figured out how to handle a dual (or multi-) monitor setup.
The first thing to understand is that there is a virtual desktop width
(and height) and that you have to place your userform according to this.

The following API's will tell you about this:

Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Private Const SM_CXVIRTUALSCREEN As Long = 78
Private Const SM_CMONITORS As Long = 80
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As
Long) As Long

Function GetDesktopMaximumWidth() As Long
If IsMultiMonitorSystem() Then
GetDesktopMaximumWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN)
Else
GetDesktopMaximumWidth = GetSystemMetrics(SM_CXSCREEN)
End If
End Function

Function IsMultiMonitorSystem() As Boolean
IsMultiMonitorSystem = GetSystemMetrics(SM_CMONITORS) > 1
End Function

Function GetMonitorCount() As Long
GetMonitorCount = GetSystemMetrics(SM_CMONITORS)
End Function

Then you can place your forms accordingly as shown in the following
code fragment:

'SystemMetrics API is in pixels
'Userform left and top is in points
'1 pixel = 15 twips
'1 point = 20 twips
'1 point = 20/15 pixels
'1 point = 1.3333333 pixels
'1 pixel = 0.75 points

With frmForm
If bDualMonitor = True Then
If strAddinScreen = "Right" Then
.StartupPosition = 0
.Top = 0
.Left = (GetDesktopMaximumWidth() * 0.75) - .Width
Else
.StartupPosition = 0
.Top = 0
.Left = 0
End If
End If
End With

Hope this clarifies it.
The best place to get information about the API's to do with this is:
http://vbnet.mvps.org/index.html?code/screen/index.html


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top