Hi,
I wrote a little set of WIN32 API wrapper C++ classes, just as a way to learn the API and to maintain my code organized.
My Window class is supposed to be used like this:
The ProcedureBag class is basically a wrapper for an STL map, holding (message-procedure) pairs. It only has two methods:
The latter is called by the Window class in its window procedure, shown below:
The problem is that I have to declare Window::WndProc as static in order for the file to compile (I don't know why), and hence I also need to declare pBag (a private ProcedureBag in Window class) also as static. And when I do this, the program compiles but doesn't link because of an unresolved reference to this pBag.
I'd like to make Window::WndProc an instance method and pBag an instance variable. How can I do this ?
If you need more code tell me and I'll post it.
Thanks in advance.
Any other suggestion about the approach I'm taking will be appreciated.
I wrote a little set of WIN32 API wrapper C++ classes, just as a way to learn the API and to maintain my code organized.
My Window class is supposed to be used like this:
Code:
#include <windows.h>
#include "wrappers.h"
Window *wn;
ProcedureBag *pb;
Button *bt;
void on_create(WPARAM, LPARAM);
void on_destroy(WPARAM, LPARAM);
void on_command(WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
pb = new ProcedureBag();
pb->addProcedure(WM_CREATE, on_create);
pb->addProcedure(WM_DESTROY, on_destroy);
pb->addProcedure(WM_COMMAND, on_command);
wn = new Window(hInst, "My window", pb);
return wn->processEvents(); //this function enters the messageloop
}
void on_create(WPARAM wParam, LPARAM lParam) {
//do something
}
void on_destroy(WPARAM wParam, LPARAM lParam) {
//do something
}
void on_command(WPARAM wParam, LPARAM lParam) {
//do something
}
The ProcedureBag class is basically a wrapper for an STL map, holding (message-procedure) pairs. It only has two methods:
Code:
/*
* Add a procedure to the bag, associating it to a particular message
*/
void ProcedureBag::addProcedure(UINT msg, PROCPTR procedure) {
(*bag)[msg] = procedure;
}
/*
* Executes the procedure associated to msg, passing to it wParam and lParam
*/
bool ProcedureBag::executeProcedure(UINT msg, WPARAM wParam, LPARAM lParam) {
if(bag->find(msg) != bag->end()) {
(*bag)[msg](wParam, lParam);
return true;
}
return false;
}
The latter is called by the Window class in its window procedure, shown below:
Code:
LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT msg, WPARAM wParam,LPARAM lParam) {
/*
if(pBag->executeProcedure(msg, wParam, lParam)) return 0;
return DefWindowProc(hWnd, msg, wParam, lParam);
}
The problem is that I have to declare Window::WndProc as static in order for the file to compile (I don't know why), and hence I also need to declare pBag (a private ProcedureBag in Window class) also as static. And when I do this, the program compiles but doesn't link because of an unresolved reference to this pBag.
I'd like to make Window::WndProc an instance method and pBag an instance variable. How can I do this ?
If you need more code tell me and I'll post it.
Thanks in advance.
Any other suggestion about the approach I'm taking will be appreciated.