|
如下代码:报错 address of MsgProcess 类型不匹配。
64位电脑,网上说,要改long为 longPtr。改了
Public Function MsgProcess(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPtr
还是不管用。
以下为网上抄的代码:
' 窗体中添加如下代码
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
Private Declare PtrSafe Function CreateMenu Lib "user32" () As Long
Private Declare PtrSafe Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare PtrSafe Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare PtrSafe Function CreatePopupMenu Lib "user32" () As Long
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const MF_STRING = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_SEPARATOR = &H800&
Dim MenuWnd As Long, Dump As Long, PopupMenuID As Long, PopupMenuWnd As Long, MenuID As Long
Private Sub UserForm_Initialize()
If Val(Application.Version) < 9 Then
hwnd = FindWindow("ThunderXFrame", Me.Caption)
Else
hwnd = FindWindow("ThunderDFrame", Me.Caption)
End If
MenuWnd = CreateMenu()
PopupMenuID = CreatePopupMenu()
Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "系统设置(&X)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 100, "保存(&S)...")
Dump = AppendMenu(PopupMenuID, MF_STRING, 101, "备份(&E)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 102, "退出(&X)")
PopupMenuID = CreatePopupMenu()
Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "会计凭证(&P)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 110, "录入(&L)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 111, "审核(&C)")
PopupMenuID = CreatePopupMenu()
Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "会计账簿(&Z)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 112, "记账(&T)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 113, "结账(&J)")
PopupMenuID = CreatePopupMenu()
Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "会计报表(&B)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 114, "资产负债表(&F)")
Dump = AppendMenu(PopupMenuID, MF_STRING, 115, "损益表(&Y)")
Dump = SetMenu(hwnd, MenuWnd)
PreWinProc = GetWindowLong(hwnd, GWL_WNDPROC)
SetWindowLong hwnd, GWL_WNDPROC, AddressOf MsgProcess '把这行注释掉就能运行,但不产生关联。
End Sub
Private Sub UserForm_Terminate()
DestroyMenu MenuWnd
DestroyMenu PopupMenuID
DestroyMenu PopupMenuWnd
SetWindowLong hwnd, GWL_WNDPROC, PreWinProc
End Sub
' 模块中的代码:
Public PreWinProc As Long, hwnd As Long
Public Declare PtrSafe 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 PtrSafe Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
Public Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Const MF_UNCHECKED = &H0&
Public Const MF_CHECKED = &H8&
Public Const MF_DISABLED = &H2&
Public Const MF_GRAYED = &H1&
Public Const MF_ENABLED = &H0&
Private Declare PtrSafe Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare PtrSafe Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MF_BYCOMMAND = &H0&
Public Function MsgProcess(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim SubMenu_hWnd As Long
Select Case wParam
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
MsgProcess = CallWindowProc(PreWinProc, hwnd, Msg, wParam, lParam)
End Select
End Function
|
|