Archived

This topic is now archived and is closed to further replies.

Ilthigore

WndProc in a class

Recommended Posts

I am writing an OOP version of the first NeHe tutorial so I can call CApp::WinMain(...) in my WinMain statement and then just write InitGL and DrawGLScene. However I am having some problems with WndProc as when I am including it in my window class, it is type "long(__stdcall CApp::*)" instead of the usual "long(__stdcall*)". How can I change my code to make this work. Here is the offending line of code and the error message:
quote:
wc.lpfnWndProc = (WNDPROC)WndProc;
quote:
C2440: ''type cast'' : cannot convert from '''' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' [\quote] and if I take out the (WNDPROC) bit the error is: [quote] C2440: ''='' : cannot convert from ''long (__stdcall CTLGLApp::*)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWN
The error message ends at "HWN" so the line obviously ran out. Any help would be cool >>>>>>>>>>>>>>>>> Ilthigore <<<<<<<<<<<<<<<<<

Share this post


Link to post
Share on other sites
The problem is that it''s a member function. There is a way round it, though.

Although it''s hidden for us, C++ member functions have an extra parameter - the first parameter is the ''this'' pointer. So, when you create a member function which takes (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam), it actually takes (CTLGLApp *this, HWND hWnd...) and so on. That''s not what windows is asking you for in the WNDCLASS structure.

The way to get around it is to use a static member function (add the ''static'' keyword in front of the line in the class declaration).

The problem with *that* is that you lose all access to the specific window object itself. There''s ways of getting round that too, and they use GetWindowLong and SetWindowLong. I''ll leave the rest to you.

Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Share this post


Link to post
Share on other sites
Thanks for the note on declaring it static but how can I access the non-static members of my class with a static function. I now get about 10 errors saying this and it is probably because I lack a pointer to the class. Is another static data member containing a pointer to each instance of the class (using an array) and another static member with a counter of each of the classes an answer? If there is a neater way (that is a little cumbersome) can someone tell me.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<

Share this post


Link to post
Share on other sites
Found out how to use GetWindowLong() thanks to the second reply. I didn''t actually notice it until I saw that I had 3 replies. Thank you to all those who replied and helped.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<

Share this post


Link to post
Share on other sites
Sorry to bring this post up again but I have run into more difficulties. I am now having access violations. It compiles OK but doesn''t work. Click HERE to download the source zip for my lib project. Any MORE help would be great again.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<

Share this post


Link to post
Share on other sites
Try passing your class object''s "this" pointer as the last parameter of the CreatWindwoEx function. This is what is supposed to get stored as USERDATA via the WM_CREATE processing code you added and restored for every other msg passed to your WndProc.

It would also help with future problems if you had some error checking in your code. Like an assert(pCApp) in your WM_CREATE processing. Or at least an if test to cause the create to fail so that the app gracefully abends.

You really should never use a pointer without first checking is viability.

Share this post


Link to post
Share on other sites