ok, I've been trying to get a hold of the WNDCLASSEX structure of the dialog-box for a day now but it's kinda poorly documented on the web. So, I put together some kind of an article about it.
I'm posting it here if someone is having the same problem =)
Ok, this is how to get a hold of the WNDCLASSEX structure when
createing a dialogbox.
This assumes visual studio 7 but may work on others too (haven't tested).
First off, create a dialogbox-template in the resource editor. Then
view the properties of the first node in the directory-tree:
+myProject.rc <-- this one
and set the 'MFC mode' to false.
This allows us to specify a 'Class Name' in the dialog editor properties. If
'MFC mode' is true this field is dissabled.
For the sake of the example code listed below we set this field to 'myDialogClass' (without the ' ' )
NOTE: you can also do this by editing the .rc file as text and typing ' CLASS "myDialogClass" ' at the right place .
Now do whatever you want with the dialogbox(add buttons etc) and
now we're set to code this baby.
Normally when creating a dialog we just use CreateDialog (or one of the other 'Create dialog'-functions)
with the changes we've made this GetLastError() will now return the #1407 which means "Can't find window class",
HWND hDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DiALOG1),hWnd,ptfnDialogProc);
err = GetLastError();
or something similar (we here assume that our dialog id is IDD_DIALOG1) .
This is,off course, because we added a 'Class Name' in the dialog-template, which is supposed
to be a 'window class'-name. As you may know, CreateDialog(..) (and the other functions) calls CreateWindowEx
to create the dialog, and now the CreateWindowEx is trying to create a window built on a window-class not
yet registered. What we need to do is to register this window-class using RegisterClassEx.
We do this by first calling the function GetClassInfoEx(..) to fill up a WNDCLASSEX structure with the standard dialog info.
Then we alter this structure as we want and set the szClassName-field to the class-name specified in the resource template.
So the code would look a little something like this:
Now, CreateDialog(..) calls CreateWindowEx(..) and since we've registered our window-class it finds it
wnd.hInstance=hInstance; //the handle to the instance
wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ID_ICON));
wnd.lpszClassName="myDialogClass"; //we set the Class Name in the template to this, remember ?
set other members if you wish
aTom = RegisterClassEx(&wnd); //aTom equals 0 if function fails
if(hDlg==NULL && aTom!=0)
hDlg = CreateDialog(hInstExe,MAKEINTRESOURCE(IDD_DIALOG1),hWnd,MainDialogProc);
//some error code here..
and creates the dialog based on our WNDCLASSEX structure.
A little thing about the "#32770" argument in the GetClassInfoEx call: this is the window-class that
CreateDialog(..) (and again the other dialog-functions) would use if the class name wan't specified.
I'll bet there are excepions but I don't know of any =)
Well, that was that..
=================== the end
this "article" probably has lotsa misses and errors in it but it's something anyway
Hope this will help someone.
oh, and if you find any errors or something that I've not understood please tell me. I'd really appriciate it .