VB.net Integer and Short Conversion, GetMessagePos ()

I am trying to use DWORD WINAPI GetMessagePos (void) on VB.net.

The function returns a DWORD (32 bit), which can be stored in the integer variable VB.net. Quote from the MSDN doc:

The x coordinate is in a lower order not greater than the return value; The y-coordinate is in the upper short circuit (both represent signed values ​​because they can take negative values ​​on systems with multiple monitors)

How can I get x and y coordinates using vb.net?

I'm currently trying

<System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True)>
Private Shared Function GetMessagePos() As Integer
End Function
Sub test()
    Dim pos As Integer = GetMessagePos()

    Try
        Dim x As Short = CShort(pos And &HFFFF)
        Dim y As Short = CShort(pos >> 16)

        MessageBox.Show(x & ", " & y)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

but I'm not sure if this is the right way to do this. I am trying to do some tests, for example

    Try
        Dim x As Short = -1
        Dim y As Short = 1

        Dim i As Int32 = (y << 16) Or x

        Dim x2 As Short = CShort(i And &HFFFF)
        Dim y2 As Short = CShort(i >> 16)

        MessageBox.Show(x & ", " & y)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

x y Short (Int16) , Int32 .
, , , .

, x-y GetMessagePos() WINAPI?

+4
1

, , . "" , GetMessagePos, GET_X_LPARAM GET_Y_LPARAM, Windows SDK. GetMessagePos, , . LOWORD HIWORD, , .

, , GET_X_LPARAM GET_Y_LPARAM C VB.NET. # , unchecked:

int GetXValue(UInt32 lParam)
{
    return unchecked((short)(long)lParam);
}

int GetYValue(UInt32 lParam)
{
    return unchecked((short)((long)lParam >> 16));
}

VB.NET # unchecked, . # , , .

VB.NET, . UInt32, . x- , . y- . Short:

Public Function GetXValue(lParam As UInt32) As Short
    Return CShort(lParam And &HFFFF)
End Function

Public Function GetYValue(lParam As UInt32) As Short
    Return CShort((lParam >> 16) And &HFFFF)
End Function

- , , , , , . C-, VB.NET , :

<StructLayout(LayoutKind.Explicit)> _
Public Structure CoordUnion

    <FieldOffset(0)> Public LParam As UInt32
    <FieldOffset(0)> Public XCoord As Short
    <FieldOffset(2)> Public YCoord As Short

    Public Sub New(lParam As UInt32)
        LParam = lParam
    End Sub

End Structure

:

Dim temp As CoordUnion = New CoordUnion(GetMessagePos())
Dim pt As Point = New Point(temp.XCoord, temp.YCoord)
' ...

, P/Invoke GetMessagePos, UInt32:

<System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True)>
Private Shared Function GetMessagePos() As UInt32
End Function

IntPtr / UInt32. , , , , lParam (, WndProc ).

+4

Source: https://habr.com/ru/post/1662683/


All Articles