Move a window partially off the top of the screen
I'm trying move a window partially off the top of the screen. However, Windows' default behavior will pop it back down after I'm done moving it. Yes, I understand why this particular functionality exists; however, I still don't want it for my window. Then I notice that I can use SetWindowPos
to set a window partially off the top of the screen, and it won't pops back down. For example SetWindowPos(...., -100, -100, ....)
will set my window above the screen 100 pixels, and it will stay there.
So, instead of moving my window normally (like sending SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE | 0x0002, 0);
, I want to use SetWindowPos
to move. My idea is to handle WM_MOUSEMOVE
and check the left mouse button is down. If the left mouse button is down, I call SetWindowPos
with some math done to current mouse position, previous mouse positions, and pass it through SetWindowPos
.
So far I have it working but not very well. First, if the mouse is move fast, it won't work at all. Second, when the mouse moves slowly, the window would shakes as it moves.
Here is my WinProc:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int lastMouseX = 0;
static int lastMouseY = 0;
switch (message) /* handle the messages */
{
case WM_LBUTTONDOWN:
lastMouseX = LOWORD(lParam);
lastMouseY = HIWORD(lParam);
break;
//case WM_NCMOUSEMOVE:
case WM_MOUSEMOVE:
if(wParam == MK_LBUTTON)
{
int currentMouseX = LOWORD(lParam);
int currentMouseY = HIWORD(lParam);
int deltaX = currentMouseX - lastMouseX;
int deltaY = currentMouseY - lastMouseY;
RECT prc;
GetWindowRe开发者_运维知识库ct(hwnd, &prc);
SetWindowPos(hwnd, NULL, prc.left + deltaX, prc.top + deltaY, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
lastMouseX = currentMouseX;
lastMouseY = currentMouseY;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Here is the full code http://pastebin.com/9A8iV5GW
Further inspecting windows messages, I see exactly where Windows moves the window back down but I still don't know what I need to do. I know it can be done, because I have seen it done in other apps.
<--I stop moving here with WM_LBUTTONUP -->
<windowMessage id="514" name="WM_LBUTTONUP">
<wParam>0x00000000</wParam>
<lParam>0x0049009E</lParam>
<returnValue>0x00000000</returnValue>
<-- And here is when windows moves it back down -->
</windowMessage><windowMessage id="534" name="WM_MOVING">
<wParam>0x00000009</wParam>
<lParam>0x003FF220</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="70" name="WM_WINDOWPOSCHANGING">
<wParam>0x00000000</wParam>
<lParam>0x003FEF98</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="36" name="WM_GETMINMAXINFO">
<wParam>0x00000000</wParam>
<lParam>0x003FEC20</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="71" name="WM_WINDOWPOSCHANGED">
<wParam>0x00000000</wParam>
<lParam>0x003FEF98</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="533" name="WM_CAPTURECHANGED">
<wParam>0x00000000</wParam>
<lParam>0x00000000</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="70" name="WM_WINDOWPOSCHANGING">
<wParam>0x00000000</wParam>
<lParam>0x003FF210</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage><windowMessage id="562" name="WM_EXITSIZEMOVE">
<wParam>0x00000000</wParam>
<lParam>0x00000000</lParam>
<returnValue>0x00000000</returnValue>
</windowMessage>
This works for me (designed using your captured window messages):
case WM_ENTERSIZEMOVE:
in_movesize_loop = true;
inhibit_movesize_loop = false;
break;
case WM_EXITSIZEMOVE:
in_movesize_loop = false;
inhibit_movesize_loop = false;
break;
case WM_CAPTURECHANGED:
inhibit_movesize_loop = in_movesize_loop;
break;
case WM_WINDOWPOSCHANGING:
if (inhibit_movesize_loop) {
WINDOWPOS* wp = reinterpret_cast<WINDOWPOS*>(lParam);
wp->flags |= SWP_NOMOVE;
return 0;
}
break;
精彩评论