Problem with WndProc
I'm trying to write a wrapper class for creating generalized window. I was trying to have WndProc as a member function of the class, and then assign WndProc like this:
void GLWindow::CreateWindow()
{
...
WNDCLASS WC;
WC.lpfnWndProc = (WNDPROC) (this->WndProc);
...
}
where WndProc is a member function of GLWindow class
However this gave me cast error, saying it cannot cast it that specific type.
Can anyone tell me how I can do this? Thanks.
You cannot do this. This is because when you fill WNDCLASSEX it expects a simple function pointer to MsgProc. The function pointer to the MsgProc you supply is Class::Function(). Also note this is un castable (in my experience).
I wrote a wrapper for the window creation years ago and the way i did it was to just have this MsgProc floating arounf in the cpp file. I wasn't and am still not satisfied with this.
E
ace
I wrote a wrapper for the window creation years ago and the way i did it was to just have this MsgProc floating arounf in the cpp file. I wasn't and am still not satisfied with this.
E
ace
Why not using a static function ? Look at this :
When you create your window with CreateWindowEx() set lpParam to 'this'. You'll then be able to get it back in your static function.
//
//
//
class cWindow
{
public:
.....
private:
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
}
//
//
//
LRESULT CALLBACK cWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Get back our 'this'
cWindow* pThis = (cWindow*)GetWindowLong(hWnd, GWL_USERDATA);
switch (uMsg)
{
// Window creation
case WM_CREATE:
{
// class pointer
pThis = (cWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
// Store it in the window's info
SetWindowLong(hWnd, GWL_USERDATA, (LONG )pThis);
}
break;
......
}
Hope it'll help you..
When you create your window with CreateWindowEx() set lpParam to 'this'. You'll then be able to get it back in your static function.
//
//
//
class cWindow
{
public:
.....
private:
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
}
//
//
//
LRESULT CALLBACK cWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Get back our 'this'
cWindow* pThis = (cWindow*)GetWindowLong(hWnd, GWL_USERDATA);
switch (uMsg)
{
// Window creation
case WM_CREATE:
{
// class pointer
pThis = (cWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
// Store it in the window's info
SetWindowLong(hWnd, GWL_USERDATA, (LONG )pThis);
}
break;
......
}
Hope it'll help you..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement