Advertisement

Creating a window with a class -> error [win32 c++]

Started by February 15, 2004 04:01 AM
14 comments, last by Ruudje 21 years ago
Another option, instead of using maps, is to use the lpParam parameter of CreateWindow() to store the this pointer. You just pass this to CreateWindow() in that last parameter, and then in your static WndProc, you do this:
LRESULT CALLBACK WindowProc_Static(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){  CWindow* pWindow;  pWindow = (CWindow*)GetWindowLong(hwnd, GWL_USERDATA);  return pWindow->WindowProc(hwnd, uMsg, wParam, lParam);}

You need to define two WndProcs, WindowProc_Static, and WindowProc, in your CWindow class. The static one is just that, and the other is a normal member function, which will have access to your member variables, etc., and it will do all the typical WndProc stuff.

Maybe I should write more to be clear, just in case:

//The CWindow class would look something like this:class CWindow{  public:    //Stuff    static LRESULT CALLBACK WindowProc_Static(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)    {      CWindow* pWindow;      pWindow = (CWindow*)GetWindowLong(hwnd, GWL_USERDATA);      return pWindow->WindowProc(hwnd, uMsg, wParam, lParam);    }    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)    {      //Standard message handling    }};//////The CreateWindow call would look something like this assuming that CWindow is creating its own window):wc.//Blah...wc.lpfnWndProc = (WNDPROC)(CWindow::WindowProc_Static);wc.//Blah...CreateWindow(.../*Parameters*/, hInstance, this);////If CWindowFactory is actually making the call to CreateWindow(), then I suppose it''d be more like:CWindow* pWindow = new CWindow();CreateWindow(.../*Parameters*/, hInstance, pWindow);

Microsoft very likely designed that extra parameter for purposes very much like this purpose. Back in the day, it may have been used as a pointer to a structure, rather than a class, but it''s all the same concept, nonetheless.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
If I recall correctly, passing this in CreateWindowEx() will only send that information in the WM_CREATE message, since it is only additional data that is used to create the window. It won''t be stored in GWL_USERDATA.

I believe to actually set the GWL_USERDATA, you need a call to either SetWindowLong() or SetWindowLongPtr().
Advertisement
Indeed, it seems as though you''re correct. I didn''t check the documentation very well. Thanks for the correction. Regardless, though, the general idea will still work. Just call SetWindowLong() just after creating the window and the CWindow object.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
There''s an article on the subject. It''s a little different (dynamic message handling), but it covers the basics.

I should know, I wrote it. And yes, this is a shameless plug!
tnx for all the replies, but I simply do not have the time to analyse that article and get this class to work I have WAAY to little free time...
You just need to move the line:
std::map CWindowFactory::windows; 

out of the header and into the cpp file, so that the compiler only sees it once.

Enigma

This topic is closed to new replies.

Advertisement