Question

I'm not new to C++, however the concept of Win32 programming is fairly new to me. Anyway, I was having some trouble getting the X and Y coordinates when a WM_NCHITTEST message is passed through WndProc. To attempt to get them right now, I'm using the following code:

int x = LOWORD(lParam);
int y = HIWORD(lParam);

I'm not quite sure if it's working or not, but I believe it is. I read somewhere that this actually returns the x and y coordinates of the pointer on the monitor itself, not relative to the application.

When I did extensive google searching when trying to figure out how to convert it into the coordinates relative to the application, I came across this method (or function. I'm not in java anymore :P):

Point relativePos = theControl.PointToClient(pos);

where pos is the the x and y coordinates. I'd absolutely love to try this code out, however I'm running into multiple issues.

First of all, dots don't work in C++. I've tried them, and they don't work. Second of all, in Win32, I don't believe Point is defined. I can find POINT, however I'm not sure that's the same thing. Lastly, I'm honestly not quite sure how to define the point.

To explain why I need this, I'm trying to make the window draggable by only the top 50 pixels of the window.

case WM_NCHITTEST:
    hit = DefWindowProc(hWnd, message, wParam, lParam);
    if(y < 50)
        return HTCAPTION;
    break;

Anyway, your help is greatly appreciated.

Was it helpful?

Solution

Per the documentation for WM_NCHITTEST on MSDN, use GET_X_LPARAM and GET_Y_LPARAM from windowsx.h instead of LOWORD and HIWORD to get coordinate values from an LPARAM. You can convert the coordinates to client coordinates if you really want to by storing them in a POINT and then calling ScreenToClient like this:

POINT pt;
pt.x = GET_X_LPARAM(lParam);
pt.y = GET_Y_LPARAM(lParam);
ScreenToClient(hWnd, &pt);

However, I'm not sure there's a real benefit to doing that here. Notably, the client area begins inside the window frame, so (0, 0) in client coordinates is not the upper left corner of the window.

Probably what you really want is GetWindowRect, which gives you the bounding rectangle of the whole window in screen coordinates. You can figure out from there what the top 50 pixels are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top