■ 移除Excel工作簿和工作表窗口中左上角的图标以及右上角的最大化、最小化、关闭按钮并定制菜单和工具栏 最后一个程序是一个综合性的程序,它不仅移除了工作簿和工作表的图标及按钮,而且还定制了菜单和工具栏。 运行后,将出现一个自定义的工作窗口,窗口中有自定义的菜单栏和工具栏。本工作簿的后台有三个工作表,其中一个就是界面上的主工作窗口。注意,在程序运行前,应将本工作溥和图标文件62.ico放在同一文件夹中。单击“CONFIGURE”按钮将出现自定义的菜单和工具栏,界面中Excel的图标都没有了,根本看不出是Excel应用程序,并禁用右键功能。但当您单击了“RESTORE”按钮后,将恢复Excel菜单和工具栏,工作簿图标用62.ico图标代替。 程序代码如下: ****************************************************** 程序26 Option Private Module Private Const mszMenuSheetName As String = "CustomMenuBar" '工作表名 ‘******************************* Public Sub CreateCustomMenuBar() Dim cBar As CommandBar Dim cBarPop As CommandBarPopup Dim cBarButton As CommandBarButton '禁止屏幕刷新 With Application .ScreenUpdating = False '定义包含菜单数据的工作表 On Error GoTo ErrorHandle Dim wksMenuTable As Worksheet Set wksMenuTable = ThisWorkbook.Sheets(mszMenuSheetName) '工作表菜单名 Dim szMenuName As String szMenuName = wksMenuTable.Cells(1, 1).Value '从菜单中移除任何自定义菜单 Call DestroyCustomMenuBar '添加自定义菜单 Set cBar = .CommandBars.Add(szMenuName, , True, True) Dim lRow As Long Dim objMenu As Object Dim lLevel As Long Dim lNextLevel As Long Dim vPosOrSub As Variant Dim szCaption As String Dim bGroup As Boolean Dim lFaceId As Long Dim szShortCutText As String lRow = 3 '初始化开始行 '使用工作表中的数据添加菜单,菜单项目和子菜单 Do Until IsEmpty(wksMenuTable.Cells(lRow, 1)) With wksMenuTable lLevel = CLng(.Cells(lRow, 1)) szCaption = CStr(.Cells(lRow, 2)) szShortCutText = CStr(.Cells(lRow, 3)) vPosOrSub = .Cells(lRow, 4) bGroup = CBool(.Cells(lRow, 5)) lFaceId = CLng(.Cells(lRow, 6)) lNextLevel = CLng(.Cells(lRow + 1, 1)) End With Select Case lLevel Case 1 ' 菜单 Set cBarPop = Application.CommandBars(szMenuName). _ Controls.Add(msoControlPopup, , , CLng(vPosOrSub), True) cBarPop.Caption = szCaption Case 2 ' 菜单项 On Error Resume Next If lNextLevel = 3 Then Set objMenu = cBarPop.Controls.Add(msoControlPopup, , , , True) Else Set objMenu = cBarPop.Controls.Add(msoControlButton, , , , True) objMenu.OnAction = vPosOrSub End If objMenu.Caption = szCaption & Space(4) objMenu.ShortcutText = szShortCutText If lFaceId <> 0 Then objMenu.FaceId = CLng(lFaceId) If bGroup Then objMenu.BeginGroup = True Case 3 ' 子菜单 Set cBarButton = objMenu.Controls.Add(msoControlButton) cBarButton.Caption = szCaption & Space(4) cBarButton.OnAction = CStr(vPosOrSub) If lFaceId <> 0 Then cBarButton.FaceId = lFaceId If bGroup Then cBarButton.BeginGroup = True End Select lRow = lRow + 1 Loop '使自定义的菜单可见并不能移除 With cBar .Visible = True .Protection = msoBarNoChangeDock End With '移除 "AskAQuestion" 下拉表(指定版本) If Val(.Version) >= 10 Then Dim objCBarTemp As Object Set objCBarTemp = .CommandBars objCBarTemp.DisableAskAQuestionDropdown = True End If '缺省右击菜单列表 .CommandBars("Toolbar List").Enabled = False .ScreenUpdating = True End With ErrorExit: '恢复内存 Set wksMenuTable = Nothing Set cBarPop = Nothing Set objMenu = Nothing Set cBarButton = Nothing Set cBar = Nothing Exit Sub ErrorHandle: MsgBox Err.Description Resume ErrorExit End Sub ‘******************************* Public Sub DestroyCustomMenuBar() '删除自定义工具栏 Dim wksMenuTable As Worksheet Set wksMenuTable = ThisWorkbook.Sheets(mszMenuSheetName) Dim szMenuName As String szMenuName = wksMenuTable.Cells(1, 1).Value Call KillCustomMenu(szMenuName) Set wksMenuTable = Nothing End Sub ‘******************************* Private Sub KillCustomMenu(ByVal szMenuName As String) On Error Resume Next With Application .ScreenUpdating = False '删除指定的菜单 Dim cb As CommandBar For Each cb In .CommandBars If cb.Name = szMenuName Then cb.Delete Next cb '恢复右击菜单列表 .CommandBars("Toolbar List").Enabled = True '恢复 "AskAQuestion" 下拉列表 (指定版本) If Val(.Version) >= 10 Then Dim objCBarTemp As Object Set objCBarTemp = .CommandBars objCBarTemp.DisableAskAQuestionDropdown = False End If .ScreenUpdating = True End With End Sub ‘******************************* Private Sub TestCusMenu() '测试命令 MsgBox "Called from custom worksheet menubar" End Sub ‘*****插入新模块并在前面声明************ Dim exl As 定制Excel窗口 ‘******************************* Public Sub Auto_Open() Set exl = New 定制Excel窗口 With exl .Caption = "New Application" .CloseButton = False .Icon = ThisWorkbook.Path & "\62.ico" .Backdrop = "Main" .NoSelect = True .Status = "Status is Ready to Go" .Configure End With Set exl = Nothing Call CreateCustomMenuBar End Sub ‘******************************* Public Sub Auto_Close() Call DestroyCustomMenuBar Set exl = New 定制Excel窗口 exl.Restore Set exl = Nothing End Sub ‘******插入类模块************************* Private Declare Function SetWindowLong Lib "user32.dll" _ Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) _ As Long Private Declare Function GetWindowLong Lib "user32.dll" _ Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) _ As Long Private Declare Function SetWindowPos Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal hWndInsertAfter As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal cx As Long, _ ByVal cy As Long, _ ByVal wFlags As Long) _ As Long Private Declare Function FindWindowEx Lib "user32.dll" _ Alias "FindWindowExA" ( _ ByVal hWnd1 As Long, _ ByVal hWnd2 As Long, _ ByVal lpsz1 As String, _ ByVal lpsz2 As String) _ As Long Private Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByRef lpdwProcessId As Long) _ As Long Private Declare Function SendMessage Lib "user32.dll" _ Alias "SendMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) _ As Long Private Declare Function ExtractIcon Lib "shell32.dll" _ Alias "ExtractIconA" ( _ ByVal hInst As Long, _ ByVal lpszExeFileName As String, _ ByVal nIconIndex As Long) _ As Long Private Declare Function GetCurrentProcessId Lib "kernel32.dll" () _ As Long Private Declare Function GetDesktopWindow Lib "user32.dll" () _ As Long Private Const GWL_STYLE As Long = (-16) Private Const WS_MAXIMIZEBOX As Long = &H10000 Private Const WS_MINIMIZEBOX As Long = &H20000 Private Const WS_SYSMENU As Long = &H80000 Private Const HWND_TOP As Long = 0 Private Const SWP_NOMOVE As Long = &H2 Private Const SWP_NOSIZE As Long = &H1 Private Const SWP_FRAMECHANGED As Long = &H20 Private Const SWP_DRAWFRAME As Long = &H20 Private Const WM_SETICON As Long = &H80 Private Const csSettingSheet As String = "WorkspaceSettings" Private csCaption As String Private csIcon As String Private csStatus As String Private csSheet As String Private cbSysMenu As Boolean Private cbFullScreen As Boolean Private cbSelect As Boolean ‘******************************* Public Property Let Caption(ByVal CaptionText As String) csCaption = CaptionText End Property ‘******************************* Public Property Let Icon(ByVal FileName As String) csIcon = FileName End Property ‘******************************* Public Property Let Status(ByVal StatusText As String) csStatus = StatusText End Property ‘******************************* Public Property Let Backdrop(ByVal SheetName As String) csSheet = SheetName End Property ‘******************************* Public Property Let CloseButton(ByVal HasMenu As Boolean) cbSysMenu = HasMenu End Property ‘******************************* Public Property Let FullScreen(ByVal ShowFullScreen As Boolean) cbFullScreen = ShowFullScreen End Property ‘******************************* Public Property Let NoSelect(ByVal AllowSelection As Boolean) cbSelect = AllowSelection End Property
|