ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: 王某人123456

address of 问题还是解决不了.

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-26 22:57 | 显示全部楼层
要改就要改全套,不要改一半留一半,毕竟VBA回调很容易直接崩溃的。
另外建议调试好之后不要一直启用回调,等整个工程完成所有测试后再启用回调。
这个文件是根据你的附件改好的: TestMenu.zip (23.8 KB, 下载次数: 10)

TA的精华主题

TA的得分主题

发表于 2022-11-26 23:00 | 显示全部楼层
窗体代码:


  1. Option Explicit

  2. #If VBA7 Then
  3.   Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As LongPtr, ByVal lpWindowName As LongPtr) As LongPtr
  4.   Private Declare PtrSafe Function SetMenu Lib "user32" (ByVal hWnd As LongPtr, ByVal hMenu As LongPtr) As Long
  5.   Private Declare PtrSafe Function CreateMenu Lib "user32" () As LongPtr
  6.   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
  7.   Private Declare PtrSafe Function DestroyMenu Lib "user32" (ByVal hMenu As LongPtr) As Long
  8.   Private Declare PtrSafe Function CreatePopupMenu Lib "user32" () As LongPtr
  9.   #If Win64 Then
  10.     Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrW" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
  11.   #Else
  12.     Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
  13.   #End If
  14.   
  15.   Private hMenu As LongPtr, Dump As LongPtr, PopupMenuID As LongPtr, PopuphMenu As LongPtr, MenuId As LongPtr
  16. #Else
  17.   Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  18.   Private Declare Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
  19.   Private Declare Function SetMenu Lib "user32" (ByVal hWnd As Long, ByVal hMenu As Long) As Long
  20.   Private Declare Function CreateMenu Lib "user32" () As Long
  21.   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
  22.   Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
  23.   Private Declare Function CreatePopupMenu Lib "user32" () As Long
  24.   
  25.   Private hMenu As Long, Dump As Long, PopupMenuID As Long, PopuphMenu As Long, MenuId As Long
  26. #End If

  27. Private Const GWL_WNDPROC   As Long = -4&
  28. Private Const MF_STRING     As Long = &H0&
  29. Private Const MF_POPUP      As Long = &H10&
  30. Private Const MF_SEPARATOR  As Long = &H800&
  31. Private Const WM_SYSCOMMAND As Long = &H112

  32. Private Sub UserForm_Initialize()
  33.   If Val(Application.Version) < 9 Then
  34.     hWnd = FindWindow(StrPtr("ThunderXFrame"), StrPtr(Me.Caption))
  35.   Else
  36.     hWnd = FindWindow(StrPtr("ThunderDFrame"), StrPtr(Me.Caption))
  37.   End If

  38.   hMenu = CreateMenu()
  39.   PopupMenuID = CreatePopupMenu()
  40.   Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("系统设置(&X)"))
  41.   Dump = AppendMenu(PopupMenuID, MF_STRING, 100, StrPtr("保存(&S)..."))
  42.   Dump = AppendMenu(PopupMenuID, MF_STRING, 101, StrPtr("备份(&E)"))
  43.   Dump = AppendMenu(PopupMenuID, MF_SEPARATOR, 0, 0)
  44.   Dump = AppendMenu(PopupMenuID, MF_STRING, 102, StrPtr("退出(&X)"))

  45.   PopupMenuID = CreatePopupMenu()
  46.   Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计凭证(&P)"))
  47.   Dump = AppendMenu(PopupMenuID, MF_STRING, 110, StrPtr("录入(&L)"))
  48.   Dump = AppendMenu(PopupMenuID, MF_STRING, 111, StrPtr("审核(&C)"))
  49.   
  50.   PopupMenuID = CreatePopupMenu()
  51.   Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计账簿(&Z)"))
  52.   Dump = AppendMenu(PopupMenuID, MF_STRING, 112, StrPtr("记账(&T)"))
  53.   Dump = AppendMenu(PopupMenuID, MF_STRING, 113, StrPtr("结账(&J)"))
  54.   
  55.   PopupMenuID = CreatePopupMenu()
  56.   Dump = AppendMenu(hMenu, MF_STRING + MF_POPUP, PopupMenuID, StrPtr("会计报表(&B)"))
  57.   Dump = AppendMenu(PopupMenuID, MF_STRING, 114, StrPtr("资产负债表(&F)"))
  58.   Dump = AppendMenu(PopupMenuID, MF_STRING, 115, StrPtr("损益表(&Y)"))
  59.   Dump = SetMenu(hWnd, hMenu)

  60.   PreWinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf MsgProcess)        '把这行注释掉就能运行,但不产生关联。
  61. End Sub

  62. Private Sub UserForm_Terminate()
  63.   DestroyMenu hMenu
  64.   DestroyMenu PopupMenuID
  65.   DestroyMenu PopuphMenu
  66.   SetWindowLong hWnd, GWL_WNDPROC, PreWinProc
  67. End Sub

复制代码

模块代码如下:



  1. Option Explicit

  2. #If VBA7 Then
  3.   Public PreWinProc As LongPtr, hWnd As LongPtr
  4.   
  5.   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
  6.   Public Declare PtrSafe Function CheckMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
  7.   Public Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

  8.   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
  9.   Private Declare PtrSafe Function GetMenu Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
  10.   Private Declare PtrSafe Function GetSubMenu Lib "user32" (ByVal hMenu As LongPtr, ByVal nPos As Long) As LongPtr
  11. #Else
  12.   Public PreWinProc As Long, hWnd As Long
  13.   
  14.   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
  15.   Public Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
  16.   Public Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

  17.   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
  18.   Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
  19.   Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
  20. #End If

  21. Public Const MF_UNCHECKED   As Long = &H0&
  22. Public Const MF_CHECKED     As Long = &H8&
  23. Public Const MF_DISABLED    As Long = &H2&
  24. Public Const MF_GRAYED      As Long = &H1&
  25. Public Const MF_ENABLED     As Long = &H0&
  26. Private Const MF_BYCOMMAND  As Long = &H0&
  27. Private Const WM_COMMAND    As Long = &H111
  28. Private Const WM_SYSCOMMAND As Long = &H112
  29. Private Const SC_CLOSE      As Long = &HF060&

  30. Private Function MenuIdClick(ByVal MenuId As Long) As Boolean
  31.   Select Case MenuId
  32.     Case 100
  33.       MsgBox "你选择的是""保存""按钮!"
  34.     Case 101
  35.       MsgBox "你选择的是""备份""按钮!"
  36.     Case 102
  37.       'Unload UserForm1
  38.     Case 110
  39.       MsgBox "你选择的是""录入""按钮!"
  40.     Case 111
  41.       MsgBox "你选择的是""审核""按钮!"
  42.     Case 112
  43.       MsgBox "你选择的是""记账""按钮!"
  44.     Case 113
  45.       MsgBox "你选择的是""结账""按钮!"
  46.     Case 114
  47.       MsgBox "你选择的是""资产负债表""按钮!"
  48.     Case 115
  49.       MsgBox "你选择的是""损益表""按钮!"
  50.     Case Else
  51.       Exit Function
  52.   End Select
  53.   MenuIdClick = True
  54. End Function

  55. #If VBA7 Then
  56.   Public Function MsgProcess(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
  57.     If Msg = WM_COMMAND Then
  58.       If wParam = 102& Then
  59.         Msg = WM_SYSCOMMAND: wParam = SC_CLOSE: lParam = 0
  60.       ElseIf MenuIdClick(CLng(wParam And &HFFFFFFFF)) Then
  61.         Exit Function
  62.       End If
  63.     End If
  64.     MsgProcess = CallWindowProc(PreWinProc, hWnd, Msg, wParam, lParam)
  65.   End Function
  66. #Else
  67.   Public Function MsgProcess(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  68.     If Msg = WM_COMMAND Then
  69.       If wParam = 102 Then
  70.         Msg = WM_SYSCOMMAND: wParam = SC_CLOSE: lParam = 0
  71.       ElseIf MenuIdClick(CLng(wParam And &HFFFFFFFF)) Then
  72.         Exit Function
  73.       End If
  74.     End If
  75.     MsgProcess = CallWindowProc(PreWinProc, hWnd, Msg, wParam, lParam)
  76.   End Function
  77. #End If

复制代码



您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-20 19:43 , Processed in 0.028701 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表