使用vba在excel中始终显示全屏

时间:2016-06-01 11:19:03

标签: excel vba excel-vba

我希望我的excel xml始终以全屏视图显示。

为此,我编写下一个代码:

    Private Sub Workbook_Open()
          Application.WindowState = xlMaximized
          ActiveWindow.WindowState = xlMaximized
          Application.DisplayFullScreen = True
    End Sub

它工作正常,直到我最小化excel,一旦我再次最大化它在正常视图模式中显示,如何继续?有什么建议吗?主要思想是删除工具栏,因为我不希望用户与它们进行交互。

3 个答案:

答案 0 :(得分:1)

将其粘贴到工作簿模块中。它会在调整大小时最大化窗口:

Private Sub Workbook_WindowResize(ByVal Wn As Window)
    ActiveWindow.WindowState = xlMaximized
End Sub

答案 1 :(得分:0)

有一个事件你可以陷阱我尝试将它添加到你的ThisWorkbook模块

Option Explicit

Private mbToggle As Boolean
Private mlPriorState(-1 To 0) As XlWindowState


Private Sub Workbook_WindowResize(ByVal Wn As Window)

    mlPriorState(mbToggle) = Wn.WindowState
    mbToggle = Not mbToggle

    If Wn.WindowState = xlNormal And mlPriorState(mbToggle) <> xlMaximized Then
        ActiveWindow.WindowState = xlMaximized
    End If

End Sub

虽然这可能只适用于代表工作表/工作簿的窗口。我先试试这个;涉及Windows API的其他解决方案更复杂。

折叠在一些反馈中。这段代码适合我。

答案 2 :(得分:0)

Workbook_Activate将带来全屏模式,而其他模式将恢复正常模式。

Private Sub Workbook_Activate()
    On Error Resume Next
    With Application
        .DisplayFullScreen = True
        .CommandBars("Worksheet Menu Bar").Enabled = False
    End With

End Sub

Private Sub Workbook_Deactivate()
    On Error Resume Next
    With Application
        .DisplayFullScreen = False
        .CommandBars("Worksheet Menu Bar").Enabled = True
    End With

End Sub

修改
你不应该修改&#39; Windows在系统级别的工作方式。但是,如果你真的,真的必须;将以下内容添加到新模块并调用SetStyle过程。

该代码提供 UNTESTED &#39;按原样提供 - API是一种在系统级别修改Windows的方法,如果您不知道自己在做什么,可能会很危险(突然崩溃,数据文件损坏......)。

VB:

Option Explicit 

 'Related Windows API functions
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long 
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long 
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long 
Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long 
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWndLock As Long) As Long 

 'Window style constants
Private Const GWL_STYLE As Long = (-16) '// The offset of a window's style
Private Const GWL_EXSTYLE As Long = (-20) '// The offset of a window's extended style
Private Const WS_CAPTION As Long = &HC00000 '// Title bar bit
Private Const WS_SYSMENU As Long = &H80000 '// System menu bit
Private Const WS_THICKFRAME As Long = &H40000 '// Sizable frame bit
Private Const WS_MINIMIZEBOX As Long = &H20000 '// Minimize box bit
Private Const WS_MAXIMIZEBOX As Long = &H10000 '// Maximize box bit
Private Const WS_EX_TOOLWINDOW As Long = &H80 '// Tool Window: small titlebar bit

 'Constant to identify the Close menu item
Private Const SC_CLOSE As Long = &HF060 


Public Sub SetStyle() 

    Dim lStyle As Long, hMenu As Long 

     'Get the basic window style
    lStyle = GetWindowLong(Application.hWnd, GWL_STYLE) 

    If lStyle = 0 Then 
        MsgBox "Unable to determine application window handle...", vbExclamation, "Error" 
        Exit Sub 
    End If 

     '// Build up the basic window style flags for the form
     '// Uncomment the features you want...
     '// Set it True to enable, FALSE to disable
     '// The first 2 are obvious, ThickFrame controls if the Window is sizable or not.

     '// SetBit lStyle, WS_CAPTION, True
     '// SetBit lStyle, WS_SYSMENU, False
     '// SetBit lStyle, WS_THICKFRAME, False
    SetBit lStyle, WS_MINIMIZEBOX, False 
    SetBit lStyle, WS_MAXIMIZEBOX, False 

     'Set the basic window styles
    SetWindowLong Application.hWnd, GWL_STYLE, lStyle 

     'Get the extended window style
    lStyle = GetWindowLong(Application.hWnd, GWL_EXSTYLE) 


     '// Handle the close button differently
     '// If Close button is wanted
     '// hMenu = GetSystemMenu(Application.hWnd, 1)

     '// Not wanted - delete it from the control menu
    hMenu = GetSystemMenu(Application.hWnd, 0) 
    DeleteMenu hMenu, SC_CLOSE, 0& 


     'Update the window with the changes
    DrawMenuBar Application.hWnd 
    SetFocus Application.hWnd 

End Sub 


 '// Set or clear a bit from a style flag
Private Sub SetBit(ByRef lStyle As Long, ByVal lBit As Long, ByVal bOn As Boolean) 

    If bOn Then 
        lStyle = lStyle Or lBit 
    Else 
        lStyle = lStyle And Not lBit 
    End If 

End Sub