Problems extracting Mouse position from LParam in WndProc

  • Thread starter Thread starter crafuse
  • Start date Start date
C

crafuse

Hello,

I've overridden the WndProc function in my form to hand some special
behavior. Specifically, I look for the WM_NCMOUSEMOVE event to tell me
when the user is trying to move the window by draggin the title bar.
However, I am having trouble extracting the POINT structure that is
supposed to come with the message. For example:

Structure POINTS
Public x As Short
Public y As Short
End Structure

....

IF m.Msg = WM_NCMOUSEMOVE Then

Dim mousePos as New POINTS =
System.Runtime.Interopservices.Marshal.PtrToStructure(m.LParam,
GetType(POINTS))

....


End If

I continually receive a Null assignment exception. Is there a
diff/proper way to extact the POINTS struct that is supposed to come
with the System.Windows.Forms.Message structure?
 
You might try something like this:

Structure POINTS
Public x As Integer
Public y As Integer
End Structure

Const WM_NCMOUSEMOVE As Integer = &HA0

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = NCMOUSEMOVE Then
Dim xPos As Short = GetLoWord(m.LParam.ToInt32)
Dim yPos As Short = GetHiWord(m.LParam.ToInt32)
End If
MyBase.WndProc(m)
End Sub

Private Function GetLoWord(ByVal lngWord As Integer) As Short
If CBool(lngWord And &H8000) Then
Return CShort(&H8000 Or (lngWord And &H7FFF))
Else
Return CShort(lngWord And &HFFFF)
End If
End Function

Private Function GetHiWord(ByVal lngWord As Integer) As Short
If CBool(lngWord And &H80000000) Then
Return CShort((lngWord \ 65535) - 1)
Else
Return CShort(lngWord \ 65535)
End If
End Function


Stefan
 
crafuse said:
Hello,

I've overridden the WndProc function in my form to hand some special
behavior. Specifically, I look for the WM_NCMOUSEMOVE event to tell me
when the user is trying to move the window by draggin the title bar.
However, I am having trouble extracting the POINT structure that is
supposed to come with the message. For example:

Structure POINTS
Public x As Short
Public y As Short
End Structure

....

IF m.Msg = WM_NCMOUSEMOVE Then

Dim mousePos as New POINTS =
System.Runtime.Interopservices.Marshal.PtrToStructure(m.LParam,
GetType(POINTS))

....


End If

I continually receive a Null assignment exception. Is there a
diff/proper way to extact the POINTS struct that is supposed to come
with the System.Windows.Forms.Message structure?


m.lParam is not a pointer to a POINTS structure, thus the error. m.lParam
itself contains the values:

Dim x, y As Integer

x = (m.LParam.ToInt32 And &HFFFF)
y = (m.LParam.ToInt32 And &HFFFF0000) >> 16


Armin
 
Define a structure for POINT (as you already have) and use Message.GetLParam
supplying the structure type.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top