Handmade Hero»Forums»Code
20 posts
Dragging a child window within the main window

Hey guys!

I'm writing a program in C using the infamous Windows API in which the end goal is to have a main window and within it I want to make resizable textboxes that I can move around using the left mouse button, deleting them using the right mouse button and double click on them to add text. Similar to how many paint programs do it.

Here's a demo of most of the features that I want: KeWXs.gif

Unfortunately, the rectangles you see in the demo are just plain rectangles and therefore do not support text input. In order to make them support text input I have to convert them all to actual windows of the built-in edit control class. You'd think that such a task would be pretty easy to do but it turned out to be quite tricky...

First of all, because the windows I make within the main window are child windows, their coordinates are different and as a result, whenever I'm trying to move one of them, a WM_MOUSEMOVE message is generated and sent to the child window I just moved but the x and y coordinates of the cursor are relative to the child window and not the main window so the movement gets all messed up.

Secondly, for some reason, pressing the left mouse button without moving it at all when the cursor is on one of the child windows also generates a WM_MOUSEMOVE message for child window. How's that possible if a WM_MOUSEMOVE message is supposed to be generated only if a mouse was moved?

I'd appreciate it if you could share with me your wisdom about the Windows API message posting process in a parent-child windows scenario and how would you tackle this particular problem <3

Simon Anciaux
1241 posts
Dragging a child window within the main window
Edited by Simon Anciaux on

Could you share the code, so that we can have a look to try to figure things out ?

First of all, because the windows I make within the main window are child windows, their coordinates are different and as a result, whenever I'm trying to move one of them, a WM_MOUSEMOVE message is generated and sent to the child window I just moved but the x and y coordinates of the cursor are relative to the child window and not the main window so the movement gets all messed up.

I don't know anything about making UI using the win32 API, so here are a few thoughts:

  • assuming the child windows are actual windows, can't you just call DefWindowProc and let windows handle the moving windows ?
  • If you store the child windows positions somewhere, can't you do absolute_pos = child_window_pos + mouse_move_pos.
  • Have you tried capturing the mouse SetCapture.

Secondly, for some reason, pressing the left mouse button without moving it at all when the cursor is on one of the child windows also generates a WM_MOUSEMOVE message for child window. How's that possible if a WM_MOUSEMOVE message is supposed to be generated only if a mouse was moved?

WM_MOUSE_MOVE is one of the messages that are auto generated. I don't know the specifics, but it seems that if a message in the same "category" is generated (e.g. mouse related here ?), windows will auto generate a mouse move message. https://devblogs.microsoft.com/oldnewthing/20160624-00/?p=93745