Search code examples
c#winformswndproc

Get the coordinates of a WM_NCHITTEST message?


How do I get the coordinates of a WM_NCHITTEST message in C# code?
I'd love to get the fastest way, because performance is a requirement.


Solution

  • From MSDN:

    wParam
    This parameter is not used.

    lParam
    The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the screen.
    The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the screen.

    So you just need to extract the low-order and high-order words from the message's lParam:

    int x = lParam.ToInt32() & 0x0000FFFF;
    int y = (int)((lParam.ToInt32() & 0xFFFF0000) >> 16)
    Point pos = new Point(x, y);
    

    I wouldn't worry too much about performance, since these operations are just bit level arithmetic...

    Note that these coordinates are relative to the screen. If you want coordinates relative to a control (or form), you can use the PointToClient method:

    Point relativePos = theControl.PointToClient(pos);