What is the type of MouseEventArgs.X?

我只是一个虾纸丫 提交于 2019-12-12 05:16:32

问题


Working with C# in Visual Studio 2008 (.NET 3.5). Looking into System.Windows.Forms.MouseEventArgs.

I'm seeing strange behavior with long Panel when I intercept handle the MouseMove event. It appears that MouseEventArgs.X goes from 0 to 32767, and wraps around to -32768.

When I watch the variable in Visual Studio, it claims that it's of type int.

Apparently it's a 16-bit signed integer the way it's behaving. Is this true? Is this a hard limit?

Thanks!


回答1:


This probably comes from the fact that Windows Forms is basically a .NET wrapper around the C Windows API. For the WM_MOUSEMOVE and related messages (WM_LBUTTONDOWN, etc.), the mouse coordinates are sent in the LPARAM parameter. LPARAM is a typedef for LONG_PTR, which on 32-bit Windows (only) is in turn a typedef for long. As the docs for WM_MOUSEMOVE say, you can get the cursor position by using the GET_X_LPARAM and GET_Y_LPARAM macros, which return the low-order int and the high-order int, respectively.

Since the value is 32 bits to start with (on 32-bit Windows), the only way this makes sense is for the x and y coordinates to actually be 16-bit values. Based on the definition of GET_X_LPARAM in windowsx.h, I would also guess (but do not have an official source that says) that even on x64 only the lower 32 bits of the LPARAM are used for the mouse coordinates.




回答2:


Instead of using the position in the mouse move event, use:

PointToClient(Cursor.Position)

Microsoft could easily do this in their .Net wrapper. There might be a legitimate reason why they don't but it seems to work for me.



来源:https://stackoverflow.com/questions/5760827/what-is-the-type-of-mouseeventargs-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!