PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Registering a window class inside a class....

First off, I'll say it: I am new to c++, so I have very little experience with classes and the like.

Now, here's my situation. I am creating a class for a control. I'll call it clsControl. Now, my clsControl has to have it's own window independant of the main window, so it has to have it's own window class registered to define it's wndproc, right? Well, when I try to set the lpfnWndProc variable of the WNDCLASS structure to my WndProc function within the clsControl class and compile it, it gives me some type cast error crap:

error C2440: 'type cast' : cannot convert from '' to 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)'

So the code line looks like this:
Code:
WNDCLASS wc;
wc.lpfnWndProc = (WNDPROC) WndProc;

Now, if I don't use the (WNDPROC) typecast, I get a conversion error. It says:

error C2440: '=' : cannot convert from 'long (__cdecl clsControl::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)'

It seems to me that it is just impossible to have a wndproc in a class. Am I right, or just ignorant?

TIA.
What you want to do is exactly what MFC (Microsoft Foundation Classes) does. You may want to try using MFC if you want to abstract Windows to C++ classes.

MFC assigns the wndproc as follows:
wndclass.lpfnWndProc = (WNDPROC) DefWindowProc;

The function prototype is:
LRESULT FAR PASCAL DefWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);

Dan East
Thanks for the reply Dan.

I am trying to avoid MFC if at all humanly possible. Personal preference really, I like to be in complete control of the code in my app, and with MFC it is difficult to do that. Is there any other way? I tried using the BEGIN_MESSAGE_MAP() macros from the ATL, but they didn't work either.

Is there any way to change the window proc after creating the window with SetWindowLong or something? I have a feeling that it will complain about the function being in a class as well.

Thanks, Chris
I have never needed to change the WinProc on the fly, but you should be able to as you say with SetWindowLong, using the GWL_WNDPROC flag. Oddly the Windows CE docs do not list that flag in the table with the other values, but mentions it further below in the "Remarks" section.

DEXplor uses MFC a great deal. If you've ever used DEXplor, you may note that it contains several advanced hybrid controls, all using MFC (Settings Tree, Tree List, Custom Menus, custom Header controls, etc). The full source code to MFC is available (and included with the standard SDKs), which makes it easy to get down and dirty and achieve the desired control. You can always revert to object handles (HWND, etc), and use standard API calls when needed.

Dan East