-
moving a window
This turned out to be way more difficult than I thought. I just want to be able to move a window by dragging it from any point within the window. The obvious problem is that I don't know where the mouse is when it leaves the window. I was thinking that I could make an invisible window that spans the entire screen and use its WM_MOUSEMOVE to determine the mouse position, but that seems a little stupid. Maybe I'm going about this the wrong way.
-
Try this in your wndproc:
Code:
if(uMsg == WM_LBUTTONDOWN)
{
SendMessage(hWnd, WM_SYSCOMMAND, 0xF012, 0);
break;
}
hope that helps!
U.
-
What you are looking for is SetCapture.
Code:
case WM_LBUTTONDOWN:
SetCapture(hWnd);
break;
case WM_MOUSEMOVE:
//Appropriate SetWindowPos calls
break;
case WM_LBUTTONUP:
ReleaseCapture();
break;
SetCapture makes it so YOUR WINDOW handles all WM_MOUSEMOVE messages for the time it has the capture set.
-
If you are using MFC, it's quite simple really.
Override the OnNcHitTest() function of your dialog (if that's what you're writing) and just have it return HTCAPTION. That tells windows that the mouse is on the caption bar of the window no matter where it actually is over the dialog, so it moves the entire dialog when it's dragged.
Code:
UINT CSkinTestDlg::OnNcHitTest(CPoint point)
{
return HTCAPTION;//always say our mouse is on the caption (title bar) area
}
-
Hershlag, using setcapture is really unnecessary. And if you're not using MFC, then Lucky's suggestion wont work for you.
using the small piece of code i posted will work for you with anything. Just do
SendMessage(hWnd, WM_SYSCOMMAND, 0xF012, 0);
in your mouse click even handler. It's as simple as that!
U.
-
Well Lucky i'm not using MFC (as I am using mingw and can't) but I really don't see how that could possibly be easier than Uraldor's solution. Hershlag's approach is good for how I was doing things. But my way was a bit messy and it was error prone. Uraldor's way is perfect.
-
Well to be clear, master5001, I didn't say it was easier than Uraldor's solution.
Perhaps there are other MFC programmers who are looking for an alternate way to accomplish the task.
-
Uraldor,
I looked in winuser.h for the SC_ command defined as 0xF012 (as I would rather use the define than a value, should the define change) but could not find one.
I tried a search but could not find the define.
Could you point me to the #define for this command?
-
Nova,
I wish I could mate, but there is no #define for it. The logic behind it is that:
SC_MOVE + HTCAPTION = 0xF012 = 61458
I think it's just another one of MS's undocumented features.
That's the best I can do I'm afraid!
Cheers
U.
-
the way it probably should be done then is this
Code:
SendMessage(hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
just to be more readable. I was not familiar with this one but I suspect that they do the SetCapture stuff internally. Pretty slick though.