【问题标题】:How to maximize a window on a specific monitor with Excel VBA?如何使用 Excel VBA 在特定显示器上最大化窗口?
【发布时间】:2017-02-22 07:52:07
【问题描述】:

我想使用 Excel VBA 在特定屏幕上最大化窗口。

我使用了这个代码:

With ActiveWindow
    .WindowState = xlNormal
    .Left = 1200
    .WindowState = xlMaximized       
End With

如果监视器 2 位于监视器 1 的右侧,则代码有效。如果相反,则该方法失败(然后我必须使用 -1200)。

这个宏应该可以在不同的电脑上工作,我不知道系统是如何配置的。是否有可能检测连接了多少显示器并直接寻址我想要最大化窗口的相应显示器?

【问题讨论】:

  • 我认为可以使用 Windows API 来完成这项工作,但没有内置的 VBA 可以做到这一点。 .WindowState = xlMaximized 总是在放置窗口的监视器上最大化 Excel。因此,您需要首先确定这是“值得拥有”的功能还是“必须拥有”的功能,因为没有简单的方法。
  • 不幸的是,这是“必须的”。但是用某种“快速而肮脏”的解决方案就可以了。:-)
  • 这就是我想说的,没有“又快又脏”。但是看看这个答案stackoverflow.com/a/7241038/3219613 有几个代码 sn-ps 用于通过 Windows API 获取监视器信息。

标签: excel vba multiscreen


【解决方案1】:

如 cmets 中所述,您需要使用 Windows API;这是另一个(使用相对简单)API,它帮助我确定用户表单是否已移出屏幕的可见区域:GetSystemMetrics Lib "User32"

根据Office版本声明函数:

#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

【讨论】:

    【解决方案2】:

    我不知道 Application.Right 是否是一个选项,但我用 -1200 替换了 1200,这对我有用。

    Application.WindowState = xlNormal
    Application.Left = -1200
    Application.WindowState = xlMaximized
    

    【讨论】:

    • 像一个魅力一样让我的 Outlook 日历在我最左边的显示器中打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2022-01-17
    相关资源
    最近更新 更多