Archived

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

passing a class method as a window procedure

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

suppose i have a class like this: class Class { public: /*stuff*/ LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); }; and then when i initialize a window like this: WNDCLASSEX wc; wc.... = .... wc.lpfnWndProc = ClassObject.WndProc; it doesn''t work. is there any way of doing it?

Share this post


Link to post
Share on other sites
The WNDPROC must be static and then you have to pass the this pointer to the static WNDPROC either by using the last parameter on CreateWindoeEx or storing this in GWL_USERDATA by calling GetWindowLong/SetWindowLong. This topic has been discussed quite a few times, try to search before posting.

Share this post


Link to post
Share on other sites
Basically you should do something like this:

class Window
{
protected:
static long CALLBACK dispatch(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// check if a new window is being created

if(msg == WM_NCCREATE)
{
// retrieve pointer to window object

Window* wnd = (Window*)((LPCREATESTRUCT)lParam)->lpCreateParams;
// set handle as it will be used in the class before the CreateWindow is returns..

wnd->hWnd = hWnd;
// create a new property for this window and save this pointer

SetProp(hWnd, "_", wnd);
}

// retrieve the window object and call it's window proc

Window* wnd = (Window*)GetProp(hWnd, "_");
if(wnd)
return wnd->wndProc(msg, wParam, lParam);

return 0L;
}

virtual long wndProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
// does nothing, yet

return defWndProc(msg, wParam, lParam);
}

virtual long defWndProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(this->hWnd, msg, wParam, lParam);
}



bool create(.../*params*/)
{
hWnd = CreateWindow(....., this);
}
};


Oh and I use SetProp instead of SetWindowLong as many other uses. This is because if you use SetWindowLong for anything else, your app will crash.

[edited by - angry on November 11, 2003 11:54:22 PM]

Share this post


Link to post
Share on other sites
Short answer : You can''t. Pointers to non-static member functions are NOT compatible with pointers to non-member ("C") functions.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
you can also make the function friend of the class like this:

class MyClass
{
private:
friend LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
};

this way, WndProc will be able to mofidy members that are protected.

hope that helps
Matt

[edited by - lemurion on November 11, 2003 12:01:22 AM]

Share this post


Link to post
Share on other sites