Get the coordinates of a WM_NCHITTEST message?

前端 未结 3 1210
孤城傲影
孤城傲影 2020-12-18 00:45

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.

3条回答
  •  不思量自难忘°
    2020-12-18 01:47

    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);
    

提交回复
热议问题