189 8069 5689

消息处理vb.net 消息处理器作用

什么是子类化,有VB或者.NET的具体实例吗

1. 何谓子类化(subclassing)众所周知,Windows是一个基于消息的系统,消息在Windows的对象之间进行着传递。子类化和Windows的钩子机制存在于消息系统之中,我们可以利用这些机制来操纵、修改甚至丢弃那些在操作系统或是进程中传递的消息,以求改变系统的一些行为。子类化技术用来截取窗口或控件之间的消息,当然是消息在到达目的窗口之前完成的操作。这些被截获的消息既可以保留也可以修改它们的状态,之后就继续发送到目的地。子类化技术实现了一些正常情况下无法实现的功能,试想鼠标右键单击TextBox,系统默认弹出Undo、Cut、Copy、Paste等菜单,我们就可以利用子类化技术来改变这个系统菜单。简单的说,子类化就是创建一个新的窗口消息处理过程,并将其插入到原先的默认窗口消息处理过程之前。子类化分为三类:实例子类化(instance subclassing)—从窗口或控件的单一实例截获消息,这种子类化技术最普遍;全局子类化(global subclassing)—能够截获从相同的窗口类创建出来的多个窗口或控件的消息;超类化(superclassing)—和全局子类化很类似,区别在于可以应用在新的窗口类上面。 2. Visual Basic 6子类化的实现在Visual Basic 6子类化的实现中我将通过一段代码的实例来介绍这一技术在VB6中的应用。下面的例子将演示如何将About加入窗口的系统菜单。①创建工程启动Visual Basic 6同时创建一个标准EXE工程。 ②在窗体中录入代码Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As LongPrivate Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long Private Const MF_BYCOMMAND = H0Private Const MF_BYPOSITION = H400Private Const MF_STRING = H0Private Const MF_SEPARATOR = H800 Private Sub Form_Load()InsertMenu GetSystemMenu(Me.hWnd, False), 0, MF_BYPOSITION Or MF_SEPARATOR, 2001, ""InsertMenu GetSystemMenu(Me.hWnd, False), 0, MF_BYPOSITION Or MF_STRING, 2002, "About Me(A)"'安装子类化入口Call Init(Me.hWnd)End Sub Private Sub Form_Unload(Cancel As Integer)‘卸载子类化Call Terminate(Me.hWnd)End Sub ③加入一个模块并录入代码Option Explicit Private Declare Function SetWindowLong Lib "user32" Alias _ "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _ As Long, ByVal dwNewLong As Long) As Long 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 Const GWL_WNDPROC = (-4) Dim PrevWndProc Private Const WM_SYSCOMMAND = H112Const WM_DESTROY = H2‘子类化入口Public Sub Init(hWnd As Long) PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc)End Sub‘子类化出口Public Sub Terminate(hWnd As Long) Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc)End Sub‘新的窗口消息处理过程,将被插入到默认处理过程之前Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) _ As LongIf Msg = WM_DESTROY Then Terminate (Form1.hWnd) If wParam = 2002 Then MsgBox "我是40Star", vbInformation, "hia..hia..."End If‘调用默认的窗口处理过程SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)End Function ' -- 模块结束 -- ' 但是,需要指出的是不正确的子类化是非常危险的,将导致一个General Protection Fault(GPF)错误,致使VB应用立即崩溃。 3. Visual Basic .NET子类化的实现在.NET中使用子类化技术要比VB6中简单,因为微软在.NET中已经提供了接口,不需要我们再自己SetWindowLong了,我们做的是Overrides(覆盖) WndProc过程即可。Overrides Protected Sub WndProc( ByRef m As Message )参数m实现了Windows的消息类型。下面的例子将同样演示如何将About加入窗口的系统菜单。①创建工程创建一个VB.NET的Windows Application工程。 ②录入代码Public Class Form1 Inherits System.Windows.Forms.Form ‘中间隐去了.NET自动生成的代码‘ – 引用Win32Api – ‘Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As Int32Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Int32 Private Const MF_BYCOMMAND = H0Private Const MF_BYPOSITION = H400Private Const MF_STRING = H0Private Const MF_SEPARATOR = H800Private Const WM_SYSCOMMAND = H112 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_SEPARATOR, 2001, "") '加入一条分割线‘GetSystemMenu(Me.Handle, False)是得到系统菜单的句柄,第二个参数为True的话不能改变系统菜单,所以要设为False InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_STRING, 2002, "About Me(A)") '加入About me菜单在系统菜单中 End Sub‘子类化窗口--覆盖WndProc过程 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_SYSCOMMAND Then If m.WParam.ToInt32 = 2002 Then MsgBox("我是40Star", vbInformation, "hia..hia...") End If End If ‘调用窗口默认的处理过程 MyBase.WndProc(m) End SubEnd Class

成都创新互联公司网站建设公司,提供成都网站制作、成都做网站,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!

vb.net 如何用SendMessage发送LVM_INSERTITEMA消息,以便在listview中插入新项

插入项目ListView控件自身支持的,何必用SendMessage?而且好像PostMessage更加稳定

VB.NET中如何使用FORMATMESSAGE

这里有一篇文章写得比较详细,可以参考一下 lpMsgBuf : 应该是用来接受FormatMessage()函数格式化后的消息缓冲区的地址。


当前标题:消息处理vb.net 消息处理器作用
网站URL:http://jkwzsj.com/article/ddoodie.html

其他资讯