Question

So that's what I wanna do. I load an Bitmap image like this on WM_CREATE

g_hbmCar  = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CAR));

Next is to catch if the left mouse button is pressed and if the arrow is on the image & get coordinates like this(if I'm right ):

  case WM_LBUTTONDOWN:
                {RECT rcClient;
// check if the cursor is within the image.x limits
                    if(LOWORD(lParam)>=g_carInfo.x && LOWORD(lParam)<=g_carInfo.x+g_carInfo.width)
                    { 
//check if the cursor is within the image.y limits
                        if(HIWORD(lParam)<=g_carInfo.y && HIWORD(lParam) >= g_carInfo.y + g_carInfo.height)
                        {
                            case WM_MOUSEMOVE :
                                {
                   if (wParam & MK_LBUTTON )
                        {
                      HDC  hdc = GetDC (hwnd) ;
                      g_carInfo.x +=LOWORD (lParam);
                      g_carInfo.y += HIWORD(lParam);
                      UpdateCar(&rcClient,&g_carInfo);
                      DrawCar(hdc,&rcClient,hbr, g_hbmCar, g_hbmMask,g_carInfo);
                        ReleaseDC (hwnd, hdc) ;
                        }
                                }
                        break;

                        }
                    }
                } break;

but this does not seems to work :( Any help would be appreciated. Thanks!

Was it helpful?

Solution

case WM_MOUSEMOVE will not happen while your code is executing case WM_LBUTTONDOWN. You need to break the operation up into 3 steps.

(1) If the click is within the image limits then call SetCapture. This makes sure you will get all subsequent WM_MOUSEMOVE messages, even if the mouse if moved outside your window. Return from the WM_LBUTTONDOWN handler.

(2) In the WM_MOUSEMOVE handler, if GetCapture indicates you have the mouse captured, then do your painting. Or, it might be easier and better to simply InvalidateRect, and then let the subsequent WM_PAINT paint the object.

(3) At WM_LBUTTONUP do ReleaseCapture.

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