必威注册电子书籍教程中心网文荟萃客户留言繁體中文
设为首页
加入收藏
联系必威注册
您当前的位置:92动力网 -> 教程中心 -> 编程经验 -> Visual Basic -> 教程内容 退出登录 用户管理
栏目导航
· Visual Basic· Delphi
· Visual C++· C++ Builder
· JAVA DotNet· 其他相关
热门教程
· 如何在Visual Basic...
· 使用Visual Basic操...
· 使用VB6.0设计Activ...
· VB动态调用外部函数...
· 建立一个程序员自己...
· 如何编写高质量的VB...
· [图文] 为更新到Visual Bas...
· 再谈在VB中调用VC++...
· 用VB语言编程实现JP...
· 用DTS实现SQL数据库...
相关教程
· 如何在VB5.0中简单安...
· 如何在vb 中用api函...
· 如何在Visual Basic...

如何在VB例程中接收自定义消息
作者:佚名聽聽来源:本站整理聽聽发布时间:2005-12-25 18:56:58聽聽发布人:admin

减小字体 增大字体

如何在VB例程中接收自定义消息
(录入日期:2002.06.08 阅读次数:2141) 转载不详
作者:不详

如何在VB例程中接收自定义消息


  您可以用API函数SetWindowLong指定处理消息的窗口过程(window procedure)为自定义的函数WindowProc,捕获消息ID为WM_USER+1的自定义消息或系统消息。并且,为了保证窗口能正确的响应消息,需要保存原来默认的窗口过程并在自定义函数WindowProc中调用。如下例,在按钮事件中发送自定义消息,WindowProc捕获了窗口大小变化的系统定义消息和用户自定义的消息并显示用户自定义的消息参数wParam:
在Moduel中加入以下代码,

  Option Explicit
   Private Const GWL_WNDPROC = -4
   Public Const GWL_USERDATA = (-21)
   Public Const WM_SIZE = &H5
   Public Const WM_USER = &H400
   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

  Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
   (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  Private Declare Function SetWindowLong Lib "user32" Alias _
   "SetWindowLongA" (ByVal hwnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  Public Function Hook(ByVal hwnd As Long) As Long
   Dim pOld As Long
  ‘指定自定义的窗口过程
   pOld = SetWindowLong(hwnd, GWL_WNDPROC, _
   AddressOf WindowProc)
  ‘保存原来默认的窗口过程指针
   SetWindowLong hwnd, GWL_USERDATA, pOld
   Hook = pOld
  End Function

  Public Sub Unhook(ByVal hwnd As Long, ByVal lpWndProc As Long)
   Dim temp As Long
   注释:Cease subclassing.
   temp = SetWindowLong(hwnd, GWL_WNDPROC, lpWndProc)
  End Sub

  Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long
   If uMsg = WM_SIZE Then
   ‘处理WM_SIZE消息
    MsgBox "SIZE"
   End If
   If uMsg = WM_USER + 1 Then
    MsgBox wParam
   End If
   Dim lpPrevWndProc As Long
  ‘查询原来默认的窗口过程指针
   lpPrevWndProc = GetWindowLong(hw, GWL_USERDATA)
  ‘调用原来的窗口过程
   WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg,wParam, lParam)
  End Function

  在Form中加入以下代码:

  Dim wParam As Long
  Dim lParam As Long
  Dim lResult As Long
  Private Sub Command1_Click()
   wParam = 12345
   lResult = SendMessage(Me.hwnd, WM_USER + 1, wParam, lParam)
  End Sub

  Private Sub Form_Load()
   Me.Tag = Hook(Me.hwnd)
  End Sub
  Private Sub Form_Unload(Cancel As Integer)
   Unhook Me.hwnd, Me.Tag
  End Sub

  在VB中用自定义过程捕获并处理消息的详细信息和例子请参考:

  Q170570 HOWTO: Build a Windows Message Handler with AddressOf in Visual
   http://support.microsoft.com/support/kb/articles/q170/5/70.asp

  Q168795 HOWTO: Hook Into a Window注释:s Messages Using AddressOf

   http://support.microsoft.com/support/kb/articles/q168/7/95.asp  

[] [返回上一页] [打 印] [收 藏]
下一篇教程:用VB调试串口通讯
∷相关教程评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 网站地图 - 管理登录
Copyright 2019 必威注册. All Rights Reserved .
浙ICP备05047688号