• Advertisement
Sign in to follow this  

Best way to move a borderless window

This topic is 4739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to move a borderless window when the left mouse button is held down. Returning HTCAPTION on WM_NCHITTEST works, but disables most other mouse messages (such as NCLBUTTONDOWN). I've tried using ReleaseCapture/SendMessage(WM_NCLBUTTONDOWN) to move the form, but it just sticks to the cursor. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
pseudo code

int mpx = 0;
int mpy = 0;
bool md = false;


case WM_MOUSEDOWN:
{
POINT pt;
GetCursorPos(&pt);
ClientToScreen(&pt);
mpx,mpy = pt;
CaptureCursor();
md = true;
break;
}
ase WM_MOUSEUP:
{
md = false;
break;
}
case WM_MOUSEMOVE:
{
if(md)
{
POINT pt;
GetCursorPos(&pt);
MoveWindow(pt-mpx,mpy);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
pseudo code

*** Source Snippet Removed ***



Works fine except when you click around a bit, the window disappears.

Share this post


Link to post
Share on other sites
This procedure works quite ok but there's a problem when you click exactly on a pixel on the edge of the window and moves the mouse cursor away from the window then no WM_LBUTTONUP message will be sent and isPressed will stay true until you click the window again. It can probably be easily fixed by calling TrackMouseEvent at WM_LBUTTONDOWN and then check for the WM_NCMOUSELEAVE message which is sent when the mouse cursor leaves the window (if TrackMouseEvent was called once before).


LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static bool isPressed = false;
static RECT rect;
static POINT startPt, pt;

switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;

case WM_LBUTTONDOWN:
isPressed = true;
GetWindowRect(hWnd, &rect);
GetCursorPos(&startPt);
return 0;

case WM_LBUTTONUP:
isPressed = false;
return 0;

case WM_MOUSEMOVE:
if (isPressed)
{
GetCursorPos(&pt);
MoveWindow(hWnd,
rect.left + pt.x - startPt.x,
rect.top + pt.y - startPt.y,
rect.right - rect.left,
rect.bottom - rect.top,
true);
}

return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}




\Jimmy H

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement