VB.net 整数和短整型转换,GetMessagePos()
VB.net Integer and Short conversion, GetMessagePos()
我正在尝试使用 VB.net 中的 DWORD WINAPI GetMessagePos(void) 函数。
函数 returns 一个 DWORD(32 位),可以存储在 VB.net 整数变量中。引自 MSDN 文档:
The x-coordinate is in the low-order short of the return value; the
y-coordinate is in the high-order short (both represent signed values
because they can take negative values on systems with multiple
monitors)
如何使用 vb.net 检索 x 和 y 坐标?
我正在尝试
<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
但我不确定这样做是否正确。我正在尝试做一些测试,比如
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
基本上我在 Short (Int16) 变量中编码 x 和 y 坐标,将它们放在一个 Int32 中,然后尝试解码。
但它似乎不起作用,因为它会导致溢出。
关于如何从 GetMessagePos()
WINAPI 解码 x-y 坐标的任何想法?
提取这些值时必须小心,以确保它们在多监视器系统上正常工作。 "standard" 的实现方式,就通常与 GetMessagePos
函数一起使用的内容而言,是 GET_X_LPARAM
and GET_Y_LPARAM
macros, defined in the Windows SDK headers. These are mentioned specifically in the GetMessagePos
documentation,并且在所有文档中都应该有类似的参考文献 return 压缩坐标。还有一个警告不要使用经典的 LOWORD
或 HIWORD
宏,因为它们将值视为无符号量,正如您在问题中提到的那样。
因此,任务本质上是将 GET_X_LPARAM
和 GET_Y_LPARAM
宏从 C 翻译成 VB.NET。将它们转换为 C# 相对简单,因为您可以利用 unchecked
关键字:
int GetXValue(UInt32 lParam)
{
return unchecked((short)(long)lParam);
}
int GetYValue(UInt32 lParam)
{
return unchecked((short)((long)lParam >> 16));
}
但是 VB.NET 没有 C# 的 unchecked
关键字的等效项,因此您必须找到其他方法来避免溢出。就个人而言,我用 C# 编写了这种互操作代码并将其粘贴到库中,这样我就可以做我认为最易读的事情。
如果您更愿意坚持使用 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)
' ...
另外请注意,我已经隐式更改了 GetMessagePos
的 P/Invoke 签名,因此它 return 是 UInt32
:
<System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True)>
Private Shared Function GetMessagePos() As UInt32
End Function
您可以像使用 UInt32
类型一样轻松地在所有这些 helper/conversion 函数中使用 IntPtr
类型。事实上,这就是您通常的编写方式,因为您通常将这些指针坐标作为 window 消息的一部分打包到 lParam
值中( 例如 ,当覆盖控件的 WndProc
方法时)。
我正在尝试使用 VB.net 中的 DWORD WINAPI GetMessagePos(void) 函数。
函数 returns 一个 DWORD(32 位),可以存储在 VB.net 整数变量中。引自 MSDN 文档:
The x-coordinate is in the low-order short of the return value; the y-coordinate is in the high-order short (both represent signed values because they can take negative values on systems with multiple monitors)
如何使用 vb.net 检索 x 和 y 坐标?
我正在尝试
<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
但我不确定这样做是否正确。我正在尝试做一些测试,比如
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
基本上我在 Short (Int16) 变量中编码 x 和 y 坐标,将它们放在一个 Int32 中,然后尝试解码。
但它似乎不起作用,因为它会导致溢出。
关于如何从 GetMessagePos()
WINAPI 解码 x-y 坐标的任何想法?
提取这些值时必须小心,以确保它们在多监视器系统上正常工作。 "standard" 的实现方式,就通常与 GetMessagePos
函数一起使用的内容而言,是 GET_X_LPARAM
and GET_Y_LPARAM
macros, defined in the Windows SDK headers. These are mentioned specifically in the GetMessagePos
documentation,并且在所有文档中都应该有类似的参考文献 return 压缩坐标。还有一个警告不要使用经典的 LOWORD
或 HIWORD
宏,因为它们将值视为无符号量,正如您在问题中提到的那样。
因此,任务本质上是将 GET_X_LPARAM
和 GET_Y_LPARAM
宏从 C 翻译成 VB.NET。将它们转换为 C# 相对简单,因为您可以利用 unchecked
关键字:
int GetXValue(UInt32 lParam)
{
return unchecked((short)(long)lParam);
}
int GetYValue(UInt32 lParam)
{
return unchecked((short)((long)lParam >> 16));
}
但是 VB.NET 没有 C# 的 unchecked
关键字的等效项,因此您必须找到其他方法来避免溢出。就个人而言,我用 C# 编写了这种互操作代码并将其粘贴到库中,这样我就可以做我认为最易读的事情。
如果您更愿意坚持使用 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)
' ...
另外请注意,我已经隐式更改了 GetMessagePos
的 P/Invoke 签名,因此它 return 是 UInt32
:
<System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True)>
Private Shared Function GetMessagePos() As UInt32
End Function
您可以像使用 UInt32
类型一样轻松地在所有这些 helper/conversion 函数中使用 IntPtr
类型。事实上,这就是您通常的编写方式,因为您通常将这些指针坐标作为 window 消息的一部分打包到 lParam
值中( 例如 ,当覆盖控件的 WndProc
方法时)。