I am trying to subclass a Window on Windows (Winapi). Please excuse my lack of knowledge, the Windows style subclassing is so different to what I know from Qt.
So I have a base class Window like this (code excerpt taken from deimos1877), here is the essential part:
class BorderlessWindow
{
enum class Style : DWORD
{
windowed = ( WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN | WS_SYSMENU ),
aero_borderless = ( WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN )
};
public:
HWND hWnd;
HINSTANCE hInstance;
BorderlessWindow( HBRUSH windowBackground, const int x, const int y, const int width, const int height );
~BorderlessWindow();
static LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
};
The part of the source file:
HWND winId = 0;
BorderlessWindow::BorderlessWindow( HBRUSH windowBackground, const int x, const int y, const int width, const int height )
: hWnd( 0 ),
hInstance( GetModuleHandle( NULL ) ),
borderless( false ),
borderlessResizeable( true ),
aeroShadow( false ),
closed( false ),
visible( false )
{
WNDCLASSEX wcx = { 0 };
wcx.cbSize = sizeof( WNDCLASSEX );
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.lpszClassName = L"WindowClass";
wcx.hbrBackground = windowBackground;
wcx.hCursor = LoadCursor( hInstance, IDC_ARROW );
RegisterClassEx( &wcx );
if ( FAILED( RegisterClassEx( &wcx ) ) ) {
throw std::runtime_error( "Couldn't register window class" );
}
hWnd = CreateWindow( L"WindowClass", L"Updater", static_cast<DWORD>( Style::windowed ), x, y, width, height, 0, 0, hInstance, nullptr );
if ( !hWnd ){
throw std::runtime_error( "Couldn't create window because of reasons" );
}
SetWindowLongPtr( hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>( this ) );
show();
}
LRESULT CALLBACK BorderlessWindow::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
std::cout<<"BorderlessWindow::WndProc"<<std::endl;
BorderlessWindow *window = reinterpret_cast<BorderlessWindow*>( GetWindowLongPtr( hWnd, GWLP_USERDATA ) );
if ( !window ) return DefWindowProc( hWnd, message, wParam, lParam );
switch ( message ) {
// ...
}
return DefWindowProc(hWnd, message, wParam, lParam);
At first, all the code from the subclass-to-be was first in the BorderlessWindow class. Essentially, I draw some images and text into the Window. In my first subclass attempt (not shown here) I did not take care of the WndProc, and I think this is the reason why the image and text have not been painted anymore. So here is my last attempt:
class Dialog : public BorderlessWindow
{
public:
explicit Dialog();
~Dialog() = default;
void updateTitlebar(HWND hwndParent);
void updateImage(HWND hwndParent);
void updateText(HWND hwndParent);
void setupContent(HWND hwndParent);
protected:
static LRESULT DialogWndProc(HWND hWnd, UINT msg, WPARAM w, LPARAM l, UINT_PTR uid, DWORD_PTR RefData){
std::cout << "Dialog::SubclassWndProc" << std::endl;
Dialog *dlg = reinterpret_cast<Dialog*>(RefData);
if ( !dlg ){
return DefSubclassProc(hWnd, msg, w, l);
}
switch (msg) {
case WM_PAINT:{
dlg->updateTitlebar(hWnd);
dlg->updateText(hWnd);
dlg->updateImage(hWnd);
break;
}
default:
return DefSubclassProc(hWnd, msg, w, l);
}
}
};
Please note that I changed the signature of the new WndProc to include the parameter UINT_PTR uid
, because initially I got this error:
invalid conversion from
'LRESULT (*)(HWND, UINT, WPARAM, LPARAM, DWORD_PTR)
{aka long int (*)
(HWND__*, unsigned int, unsigned int, long int, long unsigned int)}' to
'SUBCLASSPROC
{aka long int (__attribute__((__stdcall__)) *)
(HWND__*, unsigned int, unsigned int, long int, unsigned int, long unsigned int)}' [-fpermissive]
SetWindowSubclass(hWnd,DialogWndProc,0,(DWORD_PTR)this);
After adding the parameter, according to the headercommctrl.h
typedef LRESULT (CALLBACK *SUBCLASSPROC)(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,UINT_PTR uIdSubclass,DWORD_PTR dwRefData);
WINBOOL WINAPI SetWindowSubclass(HWND hWnd,SUBCLASSPROC pfnSubclass,UINT_PTR uIdSubclass,DWORD_PTR dwRefData);
it still does not compile, showing following error:
invalid conversion from
'LRESULT (*)(HWND, UINT, WPARAM, LPARAM, UINT_PTR, DWORD_PTR)
{aka long int (*)
(HWND__*, unsigned int, unsigned int, long int, unsigned int, long unsigned int)}' to '
SUBCLASSPROC
{aka long int (__attribute__((__stdcall__)) *)
(HWND__*, unsigned int, unsigned int, long int, unsigned int, long unsigned int)}' [-fpermissive]
SetWindowSubclass(hWnd,DialogWndProc,NULL,(DWORD_PTR)this);
The signatures are matching now, aren't they? I do not understand the error properly. Am I at least on the right track?