I am trying to process a WM_MOUSEMOVE message in C#.
What is the proper way to get an X and Y coordinate from lParam which is a type of IntPtr?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try:
(note that this was the initial version, read below for the final version)
The
uncheckednormally isn’t necessary (because the “default” c# projects are unchecked)Consider that these are the definitions of the used macros:
Where
WORD == ushort,DWORD == uint. I’m cutting some ushort->short conversions.Addendum:
one and half year later, and having experienced the “vagaries” of 64 bits .NET, I concur with Celess (but note that 99% of the Windows messages are still 32 bits for reasons of compatibility, so I don’t think the problem isn’t really big now. It’s more for the future and because if you want to do something, you should do it correctly.)
The only thing I would make different is this:
instead of doing the check “is the
IntPtr4 or 8 bytes long”, I take the worst case (8 bytes long) and castxyto along. With a little luck the double cast (tolongand then toshort/touint) will be optimized by the compiler (in the end, the explicit conversion tointofIntPtris a red herring… If you use it you are putting yourself at risk in the future. You should always use thelongconversion and then use it directly/re-cast it to what you need, showing to the future programmers that you knew what you were doing.A test example: http://ideone.com/a4oGW2 (sadly only 32 bits, but if you have a 64 bits machine you can test the same code)