繁体   English   中英

如何通过调用方法并传递返回值来正确设置Windows消息的返回值?

[英]How I could properly set the return value of a Windows Message by calling a method and passing the return value?

我正在尝试自定义处理wndproc消息的方式。

在一个类中,我有一个简单的wndproc重写的子类,当处理了一个KNOWN消息时,我使用该消息信息引发一个事件,这就是我处理事件的方式:

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

我找到了解决方案,只需要参考消息即可。

我改变了这个:

Public Event MessageIntercepted As EventHandler(Of MessageInterceptedEventArgs)

除此之外:

Public Event MessageIntercepted(ByRef m As Message, ByVal args As MessageInterceptedEventArgs)

并提出了这样的事件:

RaiseEvent MessageIntercepted(m, MessageArgs)

所以现在我可以像这样处理消息结果了:

Private WithEvents WindowsMessages As New WndProcClass(Me)

Private Sub WindowsMessageHandler(ByRef m As Message, ByVal e As WndProcClass.MessageInterceptedEventArgs) _
Handles WindowsMessages.MessageIntercepted

    If e.ID = WndProcClass.KnownMessages.WM_NCHITTEST Then

        If CType(e.Result, NCHitTest) = NCHitTest.Top Then

            m.Result = New IntPtr(NCHitTest.Caption)

        End If

    End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM