正如評論中提到的那樣,您需要使用Windows API;這是另外一個(使用比較簡單)API,幫助我確定如果用戶形式離開屏幕(S)的可視區域:基於版本GetSystemMetrics的庫「USER32」
聲明函數
#If Win64 Then 'Win64=true, Win32=true, Win16= false
Private Declare PtrSafe Function apiGetSystemMetrics Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Private Declare PtrSafe Function apiGetSystemMetrics32 Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#ElseIf Win32 Then 'Win32=true, Win16=false
Private Declare Function apiGetSystemMetrics Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#Else ' Win16=true
#End If
通用功能:辦公室
Public Function dllGetMonitors() As Long
Const SM_CMONITORS = 80
dllGetMonitors = apiGetSystemMetrics(SM_CMONITORS)
End Function
'The width of the virtual screen, in pixels.
'The virtual screen is the bounding rectangle of all display monitors
Public Function dllGetHorizontalResolution() As Long
Const SM_CXVIRTUALSCREEN = 78
dllGetHorizontalResolution = apiGetSystemMetrics(SM_CXVIRTUALSCREEN)
End Function
Public Function dllGetVerticalResolution() As Long
Const SM_CYVIRTUALSCREEN = 79
dllGetVerticalResolution = apiGetSystemMetrics(SM_CYVIRTUALSCREEN)
End Function
。
更多信息:http://msdn.microsoft.com/en-us/library/ms724385(VS.85).aspx
我用來確定形式是關閉屏幕的功能:
Private Sub checkOffScreen(ByRef frm)
Dim maxTop As Long, minLeft As Long, maxLeft As Long
Dim defaultOffset As Byte, monitors As Byte
monitors = celTotalMonitors.Value
defaultOffset = 11
minLeft = 0 - (frm.Width - defaultOffset)
If monitors = 1 And celScreenResolutionX.Value > 1280 Then
maxTop = 1180 - defaultOffset
maxLeft = 1900 - defaultOffset
Else
maxTop = 750 - defaultOffset
maxLeft = (960 * monitors) - defaultOffset
End If
With frm
'If (celFormTop.Value < 0 Or celFormTop.Value > maxTop) Or _
(celFormLeft.Value < minLeft Or celFormLeft.Value > maxLeft) Then
'If .top < 0 Or .top > maxTop Or .Left < minLeft Or .Left > maxLeft Then
If celFormTop.Value > maxTop Or celFormLeft.Value > maxLeft Then
celFormTop = defaultOffset
celFormLeft = defaultOffset
End If
If .Top > maxTop Or .left > maxLeft Then
.Top = defaultOffset
.left = defaultOffset
End If
End With
End Sub
我認爲它會使用Windows API來完成這件事是可能的,但沒有VBA內置在這樣做的方式。 '.WindowState = xlMaximized'總是在放置窗口的監視器上最大化Excel。因此,您需要首先決定這是「很好有」功能還是「必須擁有」,因爲沒有簡單的方法。 –
不幸的是,這是一個「必須擁有」。但以某種方式「快速和骯髒」的解決方案將罰款。:-) – Stefan
這就是我試圖說,沒有「快速和骯髒」。但看看這個答案http://stackoverflow.com/a/7241038/3219613有幾個代碼片段通過Windows API獲取監視器信息。 –