I am making a Windows API C++ wrapper. The header file looks like this:
#include <windows.h>
#include <exception>
#include <stdexcept>
const int NOID = -1;
class inst {
struct impinst; // pimpl idiom
impinst *imp;
friend class win; // win needs to see the private members of inst
// (namely, the WNDCLASS)
public:
inst(const char *, HINSTANCE, int=NOID,
HICON=LoadIcon(NULL, IDI_APPLICATION));
~inst();
};
class win {
struct impwidget; // pimpl idiom
impwidget *imp;
public:
win(inst &, const char *, int=0, int=0, int=600, int=450);
void show(int);
WPARAM msgpump();
~win();
};
// These are the object oriented message classes
// handler will be implemented by user of the library
class msg {
public:
virtual void handler();
};
class movemsg : public msg {
public:
void handler();
};
class sizemsg : public msg {
public:
void handler();
};
The implementation (cpp) file:
#include "winlib.h"
struct inst::impinst {
WNDCLASS wc;
static LRESULT CALLBACK winproc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp)
{
return DefWindowProc(hwnd, wm, wp, lp);
}
impinst(const char *classname, HINSTANCE hInst, int menuid, HICON hi)
{
this->wc.style = 0;
this->wc.lpfnWndProc = this->winproc;
this->wc.cbClsExtra = 0;
this->wc.cbWndExtra = 0;
this->wc.hInstance = hInst;
this->wc.hIcon = hi;
this->wc.hCursor = LoadCursor(NULL, IDC_ARROW);
this->wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
this->wc.lpszMenuName = (menuid == NOID) ?
NULL : MAKEINTRESOURCE(menuid);
this->wc.lpszClassName = classname;
if (!RegisterClass(&this->wc))
throw "Could not construct window instance";
}
};
inst::inst(const char *classname, HINSTANCE hInst, int menuid, HICON hi)
{
this->imp = new impinst(classname, hInst, menuid, hi);
}
inst::~inst()
{
delete this->imp;
}
struct win::impwidget {
HWND hwnd;
impwidget(inst &i, const char *text, int x, int y, int width, int height)
{
this->hwnd = CreateWindow(i.imp->wc.lpszClassName, text,
WS_OVERLAPPEDWINDOW, x, y, width, height,
NULL, NULL, i.imp->wc.hInstance, NULL);
if (this->hwnd == NULL)
throw "Could not create window";
}
};
win::win(inst &i, const char *text, int x, int y, int width, int height)
{
this->imp = new impwidget(i, text, x, y, width, height);
}
void win::show(int cmdshow)
{
ShowWindow(this->imp->hwnd, cmdshow);
UpdateWindow(this->imp->hwnd);
}
WPARAM win::msgpump()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
win::~win()
{
delete this->imp;
}
I have a problem though: How can I pass all the message handler functions to my inst
class, so they can be implemented in the winproc
function? Right now, it is empty (just calls DefWindowProc
), but I need it to somehow get all the user-provided implementations for the handler
function and pass them to the winproc
for handling. How could I do this? Do I need to pass a pointer to the msg
class?
Edit:
My question is different, because my question asks what to pass to the lpParam
argument of CreateWindow
, not how like in the other question.