ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 单元格与Inputbox同步显示

单元格与Inputbox同步显示

作者:绿色风 分类: 时间:2022-08-18 浏览:107
楼主
amulee
学习API过程中的一个示例。
对InputBox中的文本输入框进行子类化以拦截输入的信息,并同步显示在单元格中。

 
其实原理很简单,代码如下:
  1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  2. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal Hwnd1 As Long, ByVal Hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  4. Private Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
  5. Private Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
  6. Private Declare 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
  7. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  8. Private Const WM_DESTROY = &H2
  9. Private Const GWL_WNDPROC = (-4)
  10. Private Const WM_CHAR = &H102
  11. Dim TimerID As Long          'Timer ID
  12. Private Const InputBoxTitle = "示例"     '输入对话框标题
  13. Private lpPrevWndProc As Long '存放原窗口函数句柄
  14. '对InputBox中的文本框进行子类化
  15. Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
  16.     Dim Hwnd1 As Long, Hwnd2 As Long
  17.     '输入密码的对话框句柄
  18.     Hwnd1 = FindWindow(vbNullString, InputBoxTitle)
  19.     If Hwnd1 <> 0 Then        '若对话框存在
  20.         '获得文本框的句柄
  21.         Hwnd2 = FindWindowEx(Hwnd1, 0, "Edit", vbNullString)
  22.         '子类化
  23.         lpPrevWndProc = SetWindowLong(Hwnd2, GWL_WNDPROC, AddressOf txtWindowProc)
  24.         '关闭定时器
  25.         timeKillEvent TimerID
  26.     End If
  27. End Sub
  28. '自己的窗口函数,拦截消息
  29. Public Function txtWindowProc(ByVal hwnd As Long, ByVal uMsg As Long, _
  30.     ByVal wParam As Long, ByVal lParam As Long) As Long
  31.     Dim Temp As Long
  32.     If uMsg = WM_CHAR Then Range("A1") = Range("A1") & Chr(wParam)
  33.     '当窗口关闭时,取消子类化
  34.     If uMsg = WM_DESTROY Then
  35.         Temp = SetWindowLong(hwnd, GWL_WNDPROC, lpPrevWndProc)
  36.     End If
  37.     '调用原窗口函数,由原窗口函数处理Windows消息
  38.     txtWindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
  39. End Function
  40. '自定义MyInputBox函数
  41. Function MyInputBox(Prompt As String, Title As String)
  42.     TimerID = timeSetEvent(10, 50, AddressOf TimeProc, 1, 1)
  43.     pswdInputBox = InputBox(Prompt:=Prompt, Title:=Title)
  44. End Function
  45. Sub Main()
  46.     Dim aa
  47.     Range("A1").ClearContents
  48.     aa = MyInputBox("OK", InputBoxTitle)
  49. End Sub



单元格与InputBox同步显示.rar
2楼
goldwheat
看过啦,挺好的。

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap