I am trying to make a simple control, by creating a child window. I have already created another window successfully, who's handle is "h_parent". (My application instance is "h_instance".) I have then tried to create the child window, using the code below, but it does not work. The program freezes for a few seconds at the CreateWindowEx function, and then gives me an unhandled exception from the file <xutility>. Any ideas what I am doing wrong? Thanks.
I've figured out some more information that might help. If I set the window style to "0", then it runs fine. However, if either "WS_CHILD" or "WS_VISIBLE" are present, then the CreateWindowEx() does not work. ALl other window styles that I have tried seem to work fine. But I need WS_CHILD, because I want it to be a child window, so it is odd that it causes an error...
[in] Handle to a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.
So, you should pass in a non-NULL hMenu ID to the child window. The parent window would then use EnumChildWindows and get the ID of the child window using GetWindowLong. The window class of the child should also be different from the window class of the parent.
Have you registered the "my_control" class with RegisterClass/Ex? try "edit" or "button" to see if that works, otherwise make sure you have registered it.
Ah sorry, didn't see that bit, i have defently created child windows with a NULL hMenu before with no problems, I'm thinking the handle to the parent window must be wrong, because it works without WS_CHILD, and fails with.
Please can you show us where you call the CreateControl(hwnd, hinst)..
I suggest you call it in the parents WM_CREATE as this will ensure a valid hwnd for the parent.
I have rethought the RegisterClass issue and that could still be a possibility. If the window class wasn't registered, then CreateWindow returns NULL, so theoretically the program could still "run". That is, until you need the HWND of the newly created window. So yeah, try registering "my_control" if it hasn't already been registered.