// In your window procedure: //Define static POINT relPos;
// And paste the following code: case WM_LBUTTONDOWN: GetCursorPos(&relPos); // Get screen mouse pos! ScreenToClient(hWnd, &relPos); SetCapture(hWnd); return 0; case WM_LBUTTONUP: ReleaseCapture(); return 0; case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) { RECT rect; GetWindowRect(hWnd, &rect); // Screen relative. POINT p; // I call this function instead of using lParam, // because, sometimes you might be out of the window area. GetCursorPos(&p); ScreenToClient(hWnd, &p); // This is the trick :) // Notice we use the relPos so the window position is always(!) // relative to the initial point we started the dragging. // What acutally moves the window is that we take into calculation // the current position of the mouse inside the client area. MoveWindow(hWnd, rect.left - relPos.x + p.x, rect.top - relPos.y + p.y, rect.right - rect.left, rect.bottom - rect.top, true); } return 0; |