triumphost 120 Posting Whiz
        case WM_MOUSEMOVE:
                if (wParam & MK_LBUTTON)
                {
                    RECT WindowRect;
                    POINT CursorPos;
                    GetWindowRect(hwnd, &WindowRect);
                    GetCursorPos(&CursorPos);
                    SetWindowPos(hwnd, 0, WindowRect.left - CursorPos.x, WindowRect.top - CursorPos.y, 291, 157, 0);
                }
            break;

Hey Guys The above code isn't working for me. I'm trying to have the user Drag my borderless window but the thing just jumps to the 0, 0 immediately! Can someone explain why? Or what I should do?

Dani AI

Generated

Quick diagnostic for : the window jumps because the new position is being computed from absolute cursor coordinates instead of the movement delta. When your position math uses raw screen coordinates in the wrong way the window often ends up at very small values (top-left). Two reliable fixes follow.

A compact, safe approach is to let Windows handle dragging by faking a caption click. Call ReleaseCapture and send WM_NCLBUTTONDOWN with HTCAPTION:

case WM_LBUTTONDOWN:
    ReleaseCapture();
    SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    break;

See ReleaseCapture and WM_NCLBUTTONDOWN for details: ReleaseCapture and WM_NCLBUTTONDOWN.

If manual control is required (for constraints, snapping, etc.), capture the mouse on button-down, store the last cursor position, then on WM_MOUSEMOVE compute dx/dy = current - last and move the window by adding that delta to the window position. Use GetCursorPos/GetWindowRect to read positions and SetWindowPos with SWP_NOSIZE|SWP_NOZORDER to preserve size and z-order. Docs: GetCursorPos and SetWindowPos.

Troubleshooting tips: use SetCapture on mouse down and ReleaseCapture on up; update the stored cursor position after each move; and always work in screen coordinates (GetCursorPos/GetWindowRect) to avoid client-vs-screen mismatches.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.