【发布时间】:2013-11-21 23:58:41
【问题描述】:
我正在尝试自定义处理 wndproc 消息的方式。
在一个类中,我有一个简单的 wndproc 覆盖子,当处理一个已知消息时,我使用该消息信息引发一个事件,这就是我处理事件的方式:
Public Class Form1
Private WithEvents WndMessages As New WndProcClass(Form2)
Private Sub MessageHandler(ByVal sender As Object,
ByVal e As WndProcClass.MessageInterceptedEventArgs) _
Handles WndMessages.MessageIntercepted
If e.ID = WndProcClass.Messages.WM_CREATE Then
MsgBox(e.Result) ' Result = 0
WndMessages.ReturnValueToLastMessage(-1)
If e.ID = WndProcClass.Messages.WM_CLOSE Then
WndMessages.ReturnValueToLastMessage(0)
End If
End Sub
Private Shadows Sub Shown() Handles MyBase.Shown
Form2.Show()
End Sub
End Class
好吧,我现在想尝试的是创建一个可以为引发的消息设置返回值的方法(注意上面代码中的WndMessages.ReturnValueToLastMessage(-1)),我完全迷路了,我不知道如何实现该方法,当我尝试设置返回值时,该值不会更改始终为零。
这是 wndproc 类:
#Region " WndProc Class "
Public Class WndProcClass
Inherits NativeWindow
Implements IDisposable
#Region " Variables "
''' <summary>
''' The form to manage Windows Messages.
''' </summary>
Private WithEvents form As Form = Nothing
''' <summary>
''' Stores the message arguments.
''' </summary>
Private MessageArgs As New MessageInterceptedEventArgs
#End Region
#Region " Events "
''' <summary>
''' Event raised when a known message is processed.
''' </summary>
Public Event MessageIntercepted As EventHandler(Of MessageInterceptedEventArgs)
Public Class MessageInterceptedEventArgs : Inherits EventArgs
Public Property ID As Messages
Public Property IDWin32Hex As String
Public Property HWND As IntPtr
Public Property LParam As IntPtr
Public Property WParam As IntPtr
Public Property LParamAsCoordinate As Point
Public Property WParamAsCoordinate As Point
''' <summary>
''' Return value.
''' </summary>
Public Property Result As String
End Class
#End Region
#Region " Message Enumeration "
Public Enum Messages As Integer
''' <summary>
''' Sent as a signal that a window or an application should terminate.
''' </summary>
WM_CLOSE = &H10
End Enum
#End Region
#Region " Constructor "
Public Sub New(ByVal form As Form)
' Set the Formulary.
Me.form = form
' Assign the form handle.
SetFormHandle()
End Sub
#End Region
#Region " Event Handlers "
Private Sub SetFormHandle() _
Handles Form.HandleCreated, Form.Load, Form.Shown
Try
If Not Me.Handle.Equals(Me.form.Handle) Then
Me.AssignHandle(Me.form.Handle)
End If
Catch ' ex As InvalidOperationException
End Try
End Sub
Private Sub OnHandleDestroyed() _
Handles Form.HandleDestroyed
Me.ReleaseHandle()
End Sub
#End Region
#Region " Windows Messages Method "
Protected Overrides Sub WndProc(ByRef m As Message)
If [Enum].IsDefined(GetType(Messages), m.Msg) Then
With MessageArgs
.HWND = m.HWnd
.ID = [Enum].Parse(GetType(Messages), m.Msg)
.IDWin32Hex = "&H" & CStr(Hex(m.Msg))
.LParam = m.LParam
.WParam = m.WParam
.LParamAsCoordinate = New Point(m.LParam)
.WParamAsCoordinate = New Point(m.LParam)
.Result = m.Result
End With
RaiseEvent MessageIntercepted(Me, MessageArgs)
End If
' Return Message to base message handler.
MyBase.WndProc(m)
End Sub
#End Region
' Private LastMessage As Message
Public Sub ReturnValueToLastMessage(ByVal value As IntPtr)
' LastMessage.Result = value
' MyBase.WndProc(LastMessage)
End Sub
End Class
#End Region
【问题讨论】:
标签: .net vb.net winforms wndproc windows-messages