Creating child window

Hello,

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HWND CreateControl(HWND h_parent, HINSTANCE h_instance)
{
DWORD extended_window_style = 0;
LPCSTR window_class_name = "my_control";
LPCSTR window_name = "";
DWORD window_style = WS_CHILD | WS_VISIBLE;
int x = 0;
int y = 0;
int width = 100;
int height = 100;
HWND parent_window_handle = h_parent;
HMENU menu_handle = NULL;
HINSTANCE app_instance_handle = h_instance;
LPVOID window_creation_data = NULL;

HWND h_control = CreateWindowEx(extended_window_style, window_class_name, window_name, window_style, x, y, width, height, parent_window_handle, menu_handle, app_instance_handle, window_creation_data);

return h_control;
}
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...

Any ideas? Thanks.
I think the problem is with the hMenu parameter.

MSDN wrote:
hMenu

[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.
I was thinking that too, but then ejohns85 said "it runs fine" when using a NULL window style, so I dismissed that as a possibility.
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.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.