extracting mouse coordinate

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've found online a piece of code to extract the mouse pointer coordinates
in C#:

// msg = System.Windows.Form.Message
int x = (short)((uint)msg.LParam & 0x0000FFFF);
int y = (short)(((uint)msg.LParam & 0xFFFF0000) >> 16);

Unfortunately the code doesn't have much comments on it. Can anyone please
explain how mouse pointer coordinates is constructed and stored in LParam?
Why masks it with 0x0000FFFF or 0xFFFF0000, then right shift 16 times?

Please help, thanks!
-P
 
I'll give this a try...

The lparam of the message is an int which is 32bits. The x and y are stored
as short which is 16bits. Because the API was limited to how many parameters
are being passed you can the two short glued together when you get them.

What the code does is this:
- using the OR logic it removes the first short and leaves you with the
second short that you can use.
- the second one works almost the same way. it removes the second short
leaving the first short. however the result is that the useful bits are in
the higher part of the int so you have to shift them down and then convert it
to a short.
 
Cool!

Btw, what you meant was AND bit operation, right? So, the first short is
always Y coordinate and second short is always X coordinate, correct?

-P
 
Opps sorry - lol - not enough sleep.

Paul said:
Cool!

Btw, what you meant was AND bit operation, right? So, the first short is
always Y coordinate and second short is always X coordinate, correct?

-P
 
Back
Top