|
窗体代码:
- Option Explicit
- #If VBA7 Then
- Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As LongPtr, ByVal lpWindowName As LongPtr) As LongPtr
- Private Declare PtrSafe Function SetMenu Lib "user32" (ByVal hWnd As LongPtr, ByVal hMenu As LongPtr) As Long
- Private Declare PtrSafe Function CreateMenu Lib "user32" () As LongPtr
- Private Declare PtrSafe Function AppendMenu Lib "user32" Alias "AppendMenuW" (ByVal hMenu As LongPtr, ByVal wFlags As Long, ByVal wIDNewItem As LongPtr, ByVal lpNewItem As LongPtr) As Long
- Private Declare PtrSafe Function DestroyMenu Lib "user32" (ByVal hMenu As LongPtr) As Long
- Private Declare PtrSafe Function CreatePopupMenu Lib "user32" () As LongPtr
- #If Win64 Then
- Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrW" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
- #Else
- Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
- #End If
-
- Private hMenu As LongPtr, Dump As LongPtr, PopupMenuID As LongPtr, PopuphMenu As LongPtr, MenuId As LongPtr
- #Else
- Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
- Private Declare Function SetMenu Lib "user32" (ByVal hWnd As Long, ByVal hMenu As Long) As Long
- Private Declare Function CreateMenu Lib "user32" () As Long
- Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuW" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Long) As Long
- Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
- Private Declare Function CreatePopupMenu Lib "user32" () As Long
-
- Private hMenu As Long, Dump As Long, PopupMenuID As Long, PopuphMenu As Long, MenuId As Long
- #End If
- Private Const GWL_WNDPROC As Long = -4&
- Private Const MF_STRING As Long = &H0&
- Private Const MF_POPUP As Long = &H10&
- Private Const MF_SEPARATOR As Long = &H800&
- Private Const WM_SYSCOMMAND As Long = &H112
- Private Sub UserForm_Initialize()
- If Val(Application.Version) < 9 Then
- hWnd = FindWindow(StrPtr("ThunderXFrame"), StrPtr(Me.Caption))
- Else
- hWnd = FindWindow(StrPtr("ThunderDFrame"), StrPtr(Me.Caption))
- End If
- hMenu = CreateMenu()
- PopupMenuID = CreatePopupMenu()
- Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("系统设置(&X)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 100, StrPtr("保存(&S)..."))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 101, StrPtr("备份(&E)"))
- Dump = AppendMenu(PopupMenuID, MF_SEPARATOR, 0, 0)
- Dump = AppendMenu(PopupMenuID, MF_STRING, 102, StrPtr("退出(&X)"))
- PopupMenuID = CreatePopupMenu()
- Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计凭证(&P)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 110, StrPtr("录入(&L)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 111, StrPtr("审核(&C)"))
-
- PopupMenuID = CreatePopupMenu()
- Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计账簿(&Z)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 112, StrPtr("记账(&T)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 113, StrPtr("结账(&J)"))
-
- PopupMenuID = CreatePopupMenu()
- Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计报表(&B)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 114, StrPtr("资产负债表(&F)"))
- Dump = AppendMenu(PopupMenuID, MF_STRING, 115, StrPtr("损益表(&Y)"))
- Dump = SetMenu(hWnd, hMenu)
- PreWinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf MsgProcess) '把这行注释掉就能运行,但不产生关联。
- End Sub
- Private Sub UserForm_Terminate()
- DestroyMenu hMenu
- DestroyMenu PopupMenuID
- DestroyMenu PopuphMenu
- SetWindowLong hWnd, GWL_WNDPROC, PreWinProc
- End Sub
复制代码
模块代码如下:
- Option Explicit
- #If VBA7 Then
- Public PreWinProc As LongPtr, hWnd As LongPtr
-
- Public Declare PtrSafe Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As LongPtr, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Long
- Public Declare PtrSafe Function CheckMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
- Public Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
- Private Declare PtrSafe Function CallWindowProc Lib "user32" Alias "CallWindowProcW" (ByVal lpPrevWndFunc As LongPtr, ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
- Private Declare PtrSafe Function GetMenu Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
- Private Declare PtrSafe Function GetSubMenu Lib "user32" (ByVal hMenu As LongPtr, ByVal nPos As Long) As LongPtr
- #Else
- Public PreWinProc As Long, hWnd As Long
-
- Public Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Long
- Public Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
- Public Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
- Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcW" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
- Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
- Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
- #End If
- Public Const MF_UNCHECKED As Long = &H0&
- Public Const MF_CHECKED As Long = &H8&
- Public Const MF_DISABLED As Long = &H2&
- Public Const MF_GRAYED As Long = &H1&
- Public Const MF_ENABLED As Long = &H0&
- Private Const MF_BYCOMMAND As Long = &H0&
- Private Const WM_COMMAND As Long = &H111
- Private Const WM_SYSCOMMAND As Long = &H112
- Private Const SC_CLOSE As Long = &HF060&
- Private Function MenuIdClick(ByVal MenuId As Long) As Boolean
- Select Case MenuId
- Case 100
- MsgBox "你选择的是""保存""按钮!"
- Case 101
- MsgBox "你选择的是""备份""按钮!"
- Case 102
- 'Unload UserForm1
- Case 110
- MsgBox "你选择的是""录入""按钮!"
- Case 111
- MsgBox "你选择的是""审核""按钮!"
- Case 112
- MsgBox "你选择的是""记账""按钮!"
- Case 113
- MsgBox "你选择的是""结账""按钮!"
- Case 114
- MsgBox "你选择的是""资产负债表""按钮!"
- Case 115
- MsgBox "你选择的是""损益表""按钮!"
- Case Else
- Exit Function
- End Select
- MenuIdClick = True
- End Function
- #If VBA7 Then
- Public Function MsgProcess(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
- If Msg = WM_COMMAND Then
- If wParam = 102& Then
- Msg = WM_SYSCOMMAND: wParam = SC_CLOSE: lParam = 0
- ElseIf MenuIdClick(CLng(wParam And &HFFFFFFFF)) Then
- Exit Function
- End If
- End If
- MsgProcess = CallWindowProc(PreWinProc, hWnd, Msg, wParam, lParam)
- End Function
- #Else
- Public Function MsgProcess(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
- If Msg = WM_COMMAND Then
- If wParam = 102 Then
- Msg = WM_SYSCOMMAND: wParam = SC_CLOSE: lParam = 0
- ElseIf MenuIdClick(CLng(wParam And &HFFFFFFFF)) Then
- Exit Function
- End If
- End If
- MsgProcess = CallWindowProc(PreWinProc, hWnd, Msg, wParam, lParam)
- End Function
- #End If
复制代码
|
|