[quoted text, click to view] "Hong Kong Phooey" <NOSPAM@NOSPAM.com> wrote in message
news:eZa2T13vFHA.2556@TK2MSFTNGP15.phx.gbl...
>I wrote a control in which I overrode WndProc to implement a custom
> MouseMove. I caught the MouseMove message and passed it to a function
> which
> raises a custom event. In this function I get the x and y from the LParam
> of
> the message; x being the low order word, y being the high order word. I
> use
> two functions to get the LoWord and HiWord from the LParam
>
> Private Shared Function HiWord(ByVal Number As Integer) As Integer
> Return ((Number >> 16) And &HFFFF)
> End Function
>
> Private Shared Function LoWord(ByVal Number As Integer) As Integer
> Return (Number And &HFFFF)
> End Function
>
That code is only correct for unsigned integers. For signed integers the
sign bit gets shifted to the high order bit of the low order word by the
HiWord function. The clearest and simplest way to do this in VB.NET is to
use the BitConverter. When you want to extract types embedded in some
opaque byte structure, use BitConverter.
Private Shared Function HiWord(ByVal Number As Int32) As Int16
Return BitConverter.ToInt16(BitConverter.GetBytes(Number), 0)
End Function
Private Shared Function LoWord(ByVal Number As Int32) As Int16
Return BitConverter.ToInt16(BitConverter.GetBytes(Number), 2)
End Function
If you need to optimize this:
Private Sub ExtractWords(ByVal Number As Int32, ByRef HiWord As Int16, ByRef
LoWord As Int16)
Dim b As Byte() = BitConverter.GetBytes(Number)
HiWord = BitConverter.ToInt16(b, 0)
LoWord = BitConverter.ToInt16(b, 2)
End Sub
To do this with bit shifting, you would need to pull off the sign bit, do
the shifting, and replace the sign bit in its original location. Which is
more bit twiddling than I usually care to do.
David