Sunday, June 10, 2007

Obtaining mouse x and y coordinates from a Windows message, in C#

The lower 4 bits of LParam represent the mouse x coordinate; the upper 4 bits represent the y coordinate.
protected override void WndProc(ref Message m)
{
int x = (m.LParam.ToInt32() << 16) >>16;
int y = m.LParam.ToInt32() >> 16;

base.WndProc(ref m);
}
reference: http://www.codeproject.com/cs/miscctrl/DateTimeSlicker.asp?df=100&forumid=28242&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;exp=0&select=902128

Windows Message Handling, in C#

In C#, we can make a control (e.g., panel, button, textfield, etc) to handle Windows messages that come into the control class by overriding the WndProc method.

protected override void WndProc(ref Message m)
{
// process and/or update message
...

// let base class handle the message
base.WndProc(ref m);

// print out message to Debug
Debug.WriteLine(m);
}