bodacydo
bodacydo

Reputation: 79469

Is there a convenience function in win32 (windows.h) that would convert lParam to POINT?

I keep doing the following:

LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
    mouse.x = LOWORD(lParam);
    mouse.y = HIWORD(lParam);
    // ...
    return 0;
}

I wonder if there is a convenience method that will convert LOWORD(lParam) and HIWORD(lParam) to a Point for me? So I could do something like mouse = ToPoint(lParam)?

Upvotes: 6

Views: 3485

Answers (4)

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74410

No, but it would be trivial to roll your own:

POINT ToPoint(LPARAM lParam)
{
  POINT p={GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)};

  return p;
}

Upvotes: 7

Remy Lebeau
Remy Lebeau

Reputation: 597051

Use GET_X_LPARAM() and GET_Y_LPARAM(), or MAKEPOINTS(), like the WM_MOUSEMOVE documentation says to:

Use the following code to obtain the horizontal and vertical position:

xPos = GET_X_LPARAM(lParam);

yPos = GET_Y_LPARAM(lParam);

As noted above, the x-coordinate is in the low-order short of the return value; the y-coordinate is in the high-order short (both represent signed values because they can take negative values on systems with multiple monitors). If the return value is assigned to a variable, you can use the MAKEPOINTS macro to obtain a POINTS structure from the return value. You can also use the GET_X_LPARAM or GET_Y_LPARAM macro to extract the x- or y-coordinate.

Important Do not use the LOWORD or HIWORD macros to extract the x- and y- coordinates of the cursor position because these macros return incorrect results on systems with multiple monitors. Systems with multiple monitors can have negative x- and y- coordinates, and LOWORD and HIWORD treat the coordinates as unsigned quantities.

Upvotes: 10

mark
mark

Reputation: 5469

Use CPoint, like CPoint p(lParam);

Upvotes: 5

HerrJoebob
HerrJoebob

Reputation: 2313

Not directly but there is GET_X_LPARAM() and the corresponding for Y.

Upvotes: 1

Related Questions