Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dual monitors ?

72 views
Skip to first unread message

John A Grandy

unread,
Jul 11, 2003, 1:18:30 AM7/11/03
to
API call to determine if dual monitors are in use .... ?

in this case, i believe that Screen.Width can return the combined width of
the two monitors, and so cannot be relied on ... not sure if Screen.Width
depends on the video driver config options ....


Mike D Sutton

unread,
Jul 11, 2003, 7:13:40 AM7/11/03
to
> API call to determine if dual monitors are in use .... ?

GetSystemMetrics() is your friend:

'***
Private Declare Function GetSystemMetrics Lib _
"User32.dll" (ByVal nIndex As Long) As Long

Private Const SM_CMONITORS As Long = 80

Private Function MultiMonitors() As Boolean
MultiMonitors = (GetSystemMetrics(SM_CMONITORS) > 1)
End Function
'***

> in this case, i believe that Screen.Width can return the combined width of
> the two monitors, and so cannot be relied on ... not sure if Screen.Width
> depends on the video driver config options ....

Nope, the Screen object only retrieves the size of the primary display
monitor, to specifically get the size of the virtual screen use this
function:

'***
Private Declare Function GetSystemMetrics Lib _
"User32.dll" (ByVal nIndex As Long) As Long

Public Type RectAPI
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Const SM_XVIRTUALSCREEN As Long = 76
Private Const SM_YVIRTUALSCREEN As Long = 77
Private Const SM_CXVIRTUALSCREEN As Long = 78
Private Const SM_CYVIRTUALSCREEN As Long = 79

Public Function GetVirtualDisplayRect() As RectAPI
With GetVirtualDisplayRect
.Left = GetSystemMetrics(SM_XVIRTUALSCREEN)
.Top = GetSystemMetrics(SM_YVIRTUALSCREEN)
.Right = GetSystemMetrics(SM_CXVIRTUALSCREEN) + .Left
.Bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN) + .Top
End With
End Function
'***

Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: ED...@mvps.org
WWW: Http://www.mvps.org/EDais/


John A Grandy

unread,
Aug 19, 2003, 3:30:50 PM8/19/03
to
Ok .... but does this mean that if I want a dialog centered on the primary
monitor (or secondary monitor, for that matter) I must perform the various
calculations to determine its location in the virtual screen and then assign
those values to .Left & .Top ?

"Mike D Sutton" <Mike....@btclick.com> wrote in message
news:uxeM915R...@TK2MSFTNGP11.phx.gbl...

Mike D Sutton

unread,
Aug 19, 2003, 7:31:23 PM8/19/03
to
> Ok .... but does this mean that if I want a dialog centered on the primary
> monitor (or secondary monitor, for that matter) I must perform the various
> calculations to determine its location in the virtual screen and then
assign
> those values to .Left & .Top ?

Yes. If you just want to centre the window on the monitor it's on then you
can use some other API's:

'***
Private Declare Function MonitorFromWindow Lib "User32.dll" ( _
ByVal hWnd As Long, ByVal dwFlags As Long) As Long
Private Declare Function GetMonitorInfo Lib "User32.dll" Alias _
"GetMonitorInfoA" (ByVal hMonitor As Long, _
ByRef lpMI As MonitorInfo) As Long
Private Declare Function GetSystemMetrics Lib "user32" ( _


ByVal nIndex As Long) As Long

Private Declare Function SystemParametersInfo Lib "user32" Alias _
"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, _
ByRef lpRect As RectAPI) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, _
ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Private Type RectAPI


Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type MonitorInfo
cbSize As Long
rcMonitor As RectAPI
rcWork As RectAPI
dwFlags As Long
End Type

Private Const SPI_GETWORKAREA As Long = 48
Private Const MONITOR_DEFAULTTONEAREST As Long = &H2


Private Const SM_CMONITORS As Long = 80

Private Sub CentreWindow(ByVal inWnd As Long)
Dim MonInf As MonitorInfo
Dim WindowSize As RectAPI
Dim WorkArea As RectAPI

If (GetSystemMetrics(SM_CMONITORS) > 1) Then
MonInf.cbSize = Len(MonInf) ' Multi-monitor
Call GetMonitorInfo(MonitorFromWindow( _
inWnd, MONITOR_DEFAULTTONEAREST), MonInf)
WorkArea = MonInf.rcWork
Else ' Single monitor
Call SystemParametersInfo(SPI_GETWORKAREA, 0, WorkArea, 0)
End If

Call GetWindowRect(inWnd, WindowSize)

With WorkArea ' Convert to x/y/width/height
.Right = .Right - .Left
.Bottom = .Bottom - .Top
End With

With WindowSize ' Convert to x/y/width/height
.Right = .Right - .Left
.Bottom = .Bottom - .Top
End With

Call MoveWindow(inWnd, _
((WorkArea.Right - WindowSize.Right) \ 2) + WorkArea.Left, _
((WorkArea.Bottom - WindowSize.Bottom) \ 2) + WorkArea.Top, _
WindowSize.Right, WindowSize.Bottom, True)
End Sub

0 new messages