ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

address of 问题还是解决不了.

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-21 00:06 | 显示全部楼层 |阅读模式

如下代码:报错   address of MsgProcess 类型不匹配。
image.png

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



image.png
捕获.JPG
image.png

TA的精华主题

TA的得分主题

发表于 2022-11-21 10:03 | 显示全部楼层
括号内的怎么不改?

TA的精华主题

TA的得分主题

发表于 2022-11-21 10:36 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
这不都发现问题了嘛?

TA的精华主题

TA的得分主题

发表于 2022-11-21 10:38 来自手机 | 显示全部楼层
本帖最后由 smsn 于 2022-11-21 10:39 编辑

Private Declare PtrSafe Function MsgProcess(ByVal hwnd As LongPtr, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

TA的精华主题

TA的得分主题

发表于 2022-11-21 10:45 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
你的SetWindowLong最后一个参数类型是long与addressof不一致。 建议补一下C和VB基础,不过建议还是换32位office吧

TA的精华主题

TA的得分主题

发表于 2022-11-21 15:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
MSDN上说,要兼容32位和64位,需要用SetWindowLongPtr替代SetWindowLong,
函数语法如下:
LONG_PTR SetWindowLongPtrA(
  [in] HWND     hWnd,
  [in] int      nIndex,
  [in] LONG_PTR dwNewLong
);
对应的64位函数声明为:
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr

TA的精华主题

TA的得分主题

发表于 2022-11-22 11:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-26 15:12 | 显示全部楼层
smsn 发表于 2022-11-21 10:38
Private Declare PtrSafe Function MsgProcess(ByVal hwnd As LongPtr, ByVal Msg As Long, ByVal wParam A ...

谢谢!但是只改这个,运行后还是报一样的错。

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-26 15:13 | 显示全部楼层
本帖最后由 王某人123456 于 2022-11-26 15:18 编辑
小fisher 发表于 2022-11-21 15:15
MSDN上说,要兼容32位和64位,需要用SetWindowLongPtr替代SetWindowLong,
函数语法如下:
LONG_PTR SetW ...

谢谢您的回复。
更改后,没有报错了。但是文件直接就自动关闭了。再打开后也立即自动关闭了。

感觉进入死循环了。

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-26 15:14 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
leolee82 发表于 2022-11-21 10:45
你的SetWindowLong最后一个参数类型是long与addressof不一致。 建议补一下C和VB基础,不过建议还是换32位of ...

嗯,公司电脑,暂时换不了。 谢谢您的答复!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

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

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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