G
Guest
When using PostMessage to post myself a message, the msg and wparam
parameters somehow get swapped over. They are in the correct order when
calling PostMessage but by the time wndproc handles the message msg is now in
the wparam position and msg is set to 0 (NULL message).
Does anyone know why this behaviour is occurring ?
I've included a simple test form below to illustrate this...
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 60)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(340, 390)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Public Const WM_APP = &H8000
Public Const WM_MYTESTMSG = WM_APP + 1
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
If m.Msg = 0 Then Console.WriteLine(m.ToString)
Select Case (m.Msg)
Case WM_MYTESTMSG
MsgBox("My Test Message")
Case Else
MyBase.WndProc(m)
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lngRC As Long
lngRC = PostMessage(Me.Handle.ToInt32, WM_MYTESTMSG, 0, 0)
End Sub
End Class
parameters somehow get swapped over. They are in the correct order when
calling PostMessage but by the time wndproc handles the message msg is now in
the wparam position and msg is set to 0 (NULL message).
Does anyone know why this behaviour is occurring ?
I've included a simple test form below to illustrate this...
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 60)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(340, 390)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Public Const WM_APP = &H8000
Public Const WM_MYTESTMSG = WM_APP + 1
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
If m.Msg = 0 Then Console.WriteLine(m.ToString)
Select Case (m.Msg)
Case WM_MYTESTMSG
MsgBox("My Test Message")
Case Else
MyBase.WndProc(m)
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lngRC As Long
lngRC = PostMessage(Me.Handle.ToInt32, WM_MYTESTMSG, 0, 0)
End Sub
End Class