5

I have created a borderless window using these styles: WS_VISIBLE | WS_POPUP | WS_OVERLAPPED

The problem is that the window can't be moved. I know that I could do something like getting mouse click position, and then calculate where the window would end up everytime there comes a WM_MOUSEMOVE.

But this solution is not stable, because when I move my mouse too fast, it gets out of the window, and then it won't react. I dont want to set up a hook, because they are too slow. I have searched the internet, but nothing came up at all.

What I ideally want to, is to create a window, that is able to be moved without borders or captions, where I don't need to track the mouse myself, but where Windows does it for me, like making the whole window a caption.

help-info.de
  • 6,695
  • 16
  • 39
  • 41
  • Why does the form need to be responsive to you intentionally moving it fast? – Austin Brunkhorst Jul 06 '13 at 10:07
  • @AustinBrunkhorst Because that's how people expect windows to behave. If you started dragging a window, the window should end up where your drag ends, not at some point in-between where the mouse cursor happened to outrace the window. – jamesdlin Jul 06 '13 at 11:18
  • possible duplicate of [How do I implement dragging a window using its client area?](http://stackoverflow.com/questions/7773771/how-do-i-implement-dragging-a-window-using-its-client-area) – Hans Passant Jul 06 '13 at 11:51
  • @HansPassant: Maybe, didn't see that. But anyway, thanks for answering, jamesdlin. –  Jul 06 '13 at 12:14
  • Also a duplicate of http://stackoverflow.com/questions/7178030/cbuilder-create-a-tform-with-borderstyle-bsnone-that-is-nevertheless-movable – David Jul 06 '13 at 13:11
  • sendMessage(myWindowHandle, WM_SYSCOMMAND, 61458, 0) ; Do this on mousedown event. It is the window system move command. You can now drag the window. – Codebeat Sep 06 '15 at 02:52

2 Answers2

4

I know that I could do something like getting mouse click position, and then calculate where the window would end up everytime there comes a WM_MOUSEMOVE. But this solution is not stable, because when I move my mouse too fast, it gets out of the window, and then it won't react.

You can fix that by calling SetCapture when you receive the mouse click. You then will continue to receive WM_MOUSEMOVE even after the mouse cursor leaves your window. When the user is finished dragging and release the mouse cursor, you then should call ReleaseCapture.

What I ideally want to, is to create a window, that is able to be moved without borders or captions, where I don't need to track the mouse myself, but where Windows does it for me, like making the whole window a caption.

If you really want to do that, you could respond to the WM_NCHITTEST message and return HTCAPTION.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 2
    If the entire window should be draggable, the second option is the winner. This is the standard way of solving the problem of dragging borderless windows, and you'll find suggestions/solutions all over the web. Do make sure that you also handle `WM_SETCURSOR` to provide the correct visual hinting at your window's draggability. – Cody Gray - on strike Jul 06 '13 at 11:47
  • Thank you, the second solution is perfect - just what I was looking for. –  Jul 06 '13 at 12:08
  • Oh, and I would like to give you +1, but I do not have enough reputation, as this is my first question. :( –  Jul 06 '13 at 12:19
  • @CodyGray And if you don't want the entire window to be draggable, the `WM_NCHITTEST` handler just needs to be more selective about when it returns `HTCAPTION`. – jamesdlin Jul 06 '13 at 18:13
  • @jamesdin - btw, my old htspy introduces the topic visually and in detail: http://files.rsdn.ru/42164/htspy.zip – kero Jul 07 '13 at 05:00
4

try PostMessage(hwnd,WM_SYSCOMMAND,SC_SIZE+9,0) on WM_LBUTTONDOWN.

rustyx
  • 80,671
  • 25
  • 200
  • 267
kero
  • 678
  • 3
  • 10
  • 1
    What is `SC_SIZE+9` supposed to be? Do you mean `SC_MOVE`? – jamesdlin Jul 06 '13 at 11:08
  • 2
    SC_SIZE+9 = SC_DRAG, undocumented, just try it :-) – kero Jul 06 '13 at 11:19
  • -1 for recommending undocumented features that violate the programmer's contract, *especially* when there's a better, documented way of solving the problem – Cody Gray - on strike Jul 06 '13 at 11:44
  • 2
    Even though it's undocumented, +1 for a 1-liner solution that expresses the intent exactly (the solution `NCHITTEST` -> `HTCAPTION` is a lie, and who knows how clicking the caption will act in the future?). – rustyx Oct 05 '19 at 09:44
  • This is the best solution. It's not our fault that Microsoft forgot to document this command. – ScienceDiscoverer Aug 25 '23 at 05:37
  • @rustyx Clicking captions is a standard way of moving windows. There is no reason to change it, it's an *established* way to accomplish to task, and Microsoft goes to great lengths to avoid breaking compatibility. I disagree with "that expresses the intent exactly"; IMO this 1-liner is much more cryptic. – jamesdlin Aug 25 '23 at 22:29