c#中的Win api.从IntPtr那里得到Hi和low的消息
本文关键字:Hi low 消息 那里 Win 中的 api IntPtr | 更新日期: 2023-09-27 18:12:37
我正在尝试在c#中处理WM_MOUSEMOVE消息。
从IntPtr类型的lParam中获得X和Y坐标的正确方法是什么?
尝试:
(注意这是最初的版本,阅读下面的最终版本)
IntPtr xy = value;
int x = unchecked((short)xy);
int y = unchecked((short)((uint)xy >> 16));
unchecked
通常是不必要的(因为"默认"c#项目是未检查的)
考虑以下是所使用的宏的定义:
#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
其中WORD == ushort
, DWORD == uint
。我正在删减一些短->短的转换。
附录:
一年半之后,在经历了64位的。net的"变幻莫测"之后,我同意Celess的观点(但注意到出于兼容性的原因,99%的Windows消息仍然是32位的,所以我不认为这个问题现在不是很大。)这更多的是为了将来,因为如果你想做某事,你应该正确地去做。
我唯一想做的是:
IntPtr xy = value;
int x = unchecked((short)(long)xy);
int y = unchecked((short)((long)xy >> 16));
而不是做检查"是IntPtr
4或8字节长",我采取最坏的情况(8字节长)并将xy
转换为long
。运气好的话,双强制转换(到long
,然后到short
/到uint
)将被编译器优化(最后,IntPtr
显式转换到int
是转移注意力的…如果你使用它,你就把自己的未来置于危险之中。您应该始终使用long
转换,然后直接使用它/将其重新转换为您需要的内容,向未来的程序员显示您知道在做什么。
测试示例:http://ideone.com/a4oGW2(遗憾的是只有32位,但如果您有64位的机器,您可以测试相同的代码)
32位和64位都正确:
Point GetPoint(IntPtr _xy)
{
uint xy = unchecked(IntPtr.Size == 8 ? (uint)_xy.ToInt64() : (uint)_xy.ToInt32());
int x = unchecked((short)xy);
int y = unchecked((short)(xy >> 16));
return new Point(x, y);
}
-或-
int GetIntUnchecked(IntPtr value)
{
return IntPtr.Size == 8 ? unchecked((int)value.ToInt64()) : value.ToInt32();
}
int Low16(IntPtr value)
{
return unchecked((short)GetIntUnchecked(value));
}
int High16(IntPtr value)
{
return unchecked((short)(((uint)GetIntUnchecked(value)) >> 16));
}
也可以:
int Low16(IntPtr value)
{
return unchecked((short)(uint)value); // classic unchecked cast to uint
}
int High16(IntPtr value)
{
return unchecked((short)((uint)value >> 16));
}
-或-
int Low16(IntPtr value)
{
return unchecked((short)(long)value); // presumption about internals
} // is what framework lib uses
int High16(IntPtr value)
{
return unchecked((short)((long)value >> 16));
}
走另一条路
public static IntPtr GetLParam(Point point)
{
return (IntPtr)((point.Y << 16) | (point.X & 0xffff));
} // mask ~= unchecked((int)(short)x)
-或-
public static IntPtr MakeLParam(int low, int high)
{
return (IntPtr)((high << 16) | (low & 0xffff));
} // (IntPtr)x is same as 'new IntPtr(x)'
公认的答案是对C定义的良好翻译。如果我们只是直接处理原始的'void*',那么基本上是可以的。然而,当在。net 64位执行环境中使用'IntPtr'时,'unchecked'不会阻止从IntPtr内部抛出的转换溢出异常。未检查块不影响在IntPtr函数和操作符内部发生的转换。目前公认的答案是没有必要使用"unchecked"。然而,使用'unchecked' 绝对是必要的,因为从较大的类型强制转换为负值时总是如此。
在64位上,从接受的回答:
var xy = new IntPtr(0x0FFFFFFFFFFFFFFF);
int x = unchecked((short)xy); // <-- throws
int y = unchecked((short)((uint)xy >> 16)); // gets lucky, 'uint' implicit 'long'
y = unchecked((short)((int)xy >> 16)); // <-- throws
xy = new IntPtr(0x00000000FFFF0000); // 0, -1
x = unchecked((short)xy); // <-- throws
y = unchecked((short)((uint)xy >> 16)); // still lucky
y = (short)((uint)xy >> 16); // <-- throws (short), no longer lucky
在64位上,使用DmitryG的外推版本:
var ptr = new IntPtr(0x0FFFFFFFFFFFFFFF);
var xy = IntPtr.Size == 8 ? (int)ptr.ToInt64() : ptr.ToInt32(); // <-- throws (int)
int x = unchecked((short)xy); // fine, if gets this far
int y = unchecked((short)((uint)xy >> 16)); // fine, if gets this far
y = unchecked((short)(xy >> 16)); // also fine, if gets this far
ptr = new IntPtr(0x00000000FFFF0000); // 0, -1
xy = IntPtr.Size == 8 ? (int)ptr.ToInt64() : ptr.ToInt32(); // <-- throws (int)
对性能return IntPtr.Size == 8 ? unchecked((int)value.ToInt64()) : value.ToInt32();
IntPtr。Size属性返回一个常量作为编译时文字,如果跨程序集进行内联,则该常量是可用的。因此,JIT可以将几乎所有这些都优化出来。也可以:
return unchecked((int)value.ToInt64());
-或-
return unchecked((int)(long)value);
-或-
return unchecked((uint)value); // traditional
和所有这些都将始终调用IntPtr.ToInt64()的等效函数。ToInt64()和'operator long'也可以内联,但可能性较小。在32位版本中比Size常量的代码要多得多。我认为上面的解决方案在赛门铁克上更正确。同样重要的是要注意符号扩展的工件,它会填充所有64位,不管(long)int_val之类的东西,尽管我在这里几乎掩盖了这一点,但它可能会额外影响32位的内联。
产品使用if (Low16(wParam) == NativeMethods.WM_CREATE)) { }
var x = Low16(lParam);
var point = GetPoint(lParam);
为未来的旅行者提供的"安全"IntPtr模型。
运行这个而不将设置为32位的WIN32定义,以获得64位IntPtr行为的可靠模拟。
public struct IntPtrMock
{
#if WIN32
int m_value;
#else
long m_value;
#endif
int IntPtr_ToInt32() {
#if WIN32
return (int)m_value;
#else
long l = m_value;
return checked((int)l);
#endif
}
public static explicit operator int(IntPtrMock value) { //(short) resolves here
#if WIN32
return (int)value.m_value;
#else
long l = value.m_value;
return checked((int)l); // throws here if any high 32 bits
#endif // check forces sign stay signed
}
public static explicit operator long(IntPtrMock value) { //(uint) resolves here
#if WIN32
return (long)(int)value.m_value;
#else
return (long)value.m_value;
#endif
}
public int ToInt32() {
#if WIN32
return (int)value.m_value;
#else
long l = m_value;
return checked((int)l); // throws here if any high 32 bits
#endif // check forces sign stay signed
}
public long ToInt64() {
#if WIN32
return (long)(int)m_value;
#else
return (long)m_value;
#endif
}
public IntPtrMock(long value) {
#if WIN32
m_value = checked((int)value);
#else
m_value = value;
#endif
}
}
public static IntPtr MAKELPARAM(int low, int high)
{
return (IntPtr)((high << 16) | (low & 0xffff));
}
public Main()
{
var xy = new IntPtrMock(0x0FFFFFFFFFFFFFFF); // simulate 64-bit, overflow smaller
int x = unchecked((short)xy); // <-- throws
int y = unchecked((short)((uint)xy >> 16)); // got lucky, 'uint' implicit 'long'
y = unchecked((short)((int)xy >> 16)); // <-- throws
int xy2 = IntPtr.Size == 8 ? (int)xy.ToInt64() : xy.ToInt32(); // <-- throws
int xy3 = unchecked(IntPtr.Size == 8 ? (int)xy.ToInt64() : xy.ToInt32()); //ok
// proper 32-bit lParam, overflow signed
var xy4 = new IntPtrMock(0x00000000FFFFFFFF); // x = -1, y = -1
int x2 = unchecked((short)xy4); // <-- throws
int xy5 = IntPtr.Size == 8 ? (int)xy4.ToInt64() : xy4.ToInt32(); // <-- throws
var xy6 = new IntPtrMock(0x00000000FFFF0000); // x = 0, y = -1
int x3 = unchecked((short)xy6); // <-- throws
int xy7 = IntPtr.Size == 8 ? (int)xy6.ToInt64() : xy6.ToInt32(); // <-- throws
var xy8 = MAKELPARAM(-1, -1); // WinForms macro
int x4 = unchecked((short)xy8); // <-- throws
int xy9 = IntPtr.Size == 8 ? (int)xy8.ToInt64() : xy8.ToInt32(); // <-- throws
}
通常,对于低级鼠标处理,我使用以下帮助器(它也考虑到IntPtr大小取决于x86/x64):
//...
Point point = WinAPIHelper.GetPoint(msg.LParam);
//...
static class WinAPIHelper {
public static Point GetPoint(IntPtr lParam) {
return new Point(GetInt(lParam));
}
public static MouseButtons GetButtons(IntPtr wParam) {
MouseButtons buttons = MouseButtons.None;
int btns = GetInt(wParam);
if((btns & MK_LBUTTON) != 0) buttons |= MouseButtons.Left;
if((btns & MK_RBUTTON) != 0) buttons |= MouseButtons.Right;
return buttons;
}
static int GetInt(IntPtr ptr) {
return IntPtr.Size == 8 ? unchecked((int)ptr.ToInt64()) : ptr.ToInt32();
}
const int MK_LBUTTON = 1;
const int MK_RBUTTON = 2;
}