Window Class Encapsulation and WNDPROC
-
I cannot figure out a way to get the WndProc as a member function of my window class I am building. Any Suggestions? :confused: QMuffs
Do you mean you're designing your own C++ framework for encapsulation of Win32 windows? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Do you mean you're designing your own C++ framework for encapsulation of Win32 windows? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Yes, I'm designing my own. It has to do with the hidden 'this' argument that is passed to member functions of a class. It gives me this error. Compiling... cWindow.cpp c:\projects\cwindow\cwindow.h(86) : error C2440: 'type cast' : cannot convert from '' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' None of the functions with this name in scope match the target type Error executing cl.exe. cWindow.exe - 1 error(s), 0 warning(s) QMuffs
-
Yes, I'm designing my own. It has to do with the hidden 'this' argument that is passed to member functions of a class. It gives me this error. Compiling... cWindow.cpp c:\projects\cwindow\cwindow.h(86) : error C2440: 'type cast' : cannot convert from '' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' None of the functions with this name in scope match the target type Error executing cl.exe. cWindow.exe - 1 error(s), 0 warning(s) QMuffs
Use static member functions "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
Use static member functions "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
Yes, I'm designing my own. It has to do with the hidden 'this' argument that is passed to member functions of a class. It gives me this error. Compiling... cWindow.cpp c:\projects\cwindow\cwindow.h(86) : error C2440: 'type cast' : cannot convert from '' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' None of the functions with this name in scope match the target type Error executing cl.exe. cWindow.exe - 1 error(s), 0 warning(s) QMuffs
As you pointed out, the problem lies in the hidden
this
parameter. One usual workaround to this goes as follows. Store thethis
parameter into the internal data kept byHWND
s withSetWindowLong(hwnd,GWL_USERDATA,reinterpret_cast<LONG>(this))
and set as your window proc a static function that merely recovers thethis
parameter and forwards the call to the real, per-object window proc:class cwindow
{
cwindow(HWND hwnd) // constructor
{
SetWindowLong(hwnd,GWL_USERDATA,reinterpret_cast<LONG>(this));
}
static LRESULT CALLBACK stub_WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
cwindow object=reinterpret_cast<object*>(GetWindowLong(hwnd,GWL_USERDATA));
return object->WindowProc(hwnd,uMsg,wParam,lParam);
}
LRESULT CALLBACK stub_WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) // the "real" window proc
{
...
}
};Get the idea? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Use static member functions "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
As you pointed out, the problem lies in the hidden
this
parameter. One usual workaround to this goes as follows. Store thethis
parameter into the internal data kept byHWND
s withSetWindowLong(hwnd,GWL_USERDATA,reinterpret_cast<LONG>(this))
and set as your window proc a static function that merely recovers thethis
parameter and forwards the call to the real, per-object window proc:class cwindow
{
cwindow(HWND hwnd) // constructor
{
SetWindowLong(hwnd,GWL_USERDATA,reinterpret_cast<LONG>(this));
}
static LRESULT CALLBACK stub_WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
cwindow object=reinterpret_cast<object*>(GetWindowLong(hwnd,GWL_USERDATA));
return object->WindowProc(hwnd,uMsg,wParam,lParam);
}
LRESULT CALLBACK stub_WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) // the "real" window proc
{
...
}
};Get the idea? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo