Jump to content
  • Advertisement

Archived

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

Cipher3D

passing a class method as a window procedure

This topic is 5510 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
Advertisement
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!