29. september 2001 - 14:02
Der er
3 kommentarer
Konstruktion af en window klasse / msg handling problem
Jeg er ved at lave en klasse som kan indeholde indkapsle et window.
Klassen hedder pWindow og har en virtual funktion som hedder MsgHandler.
Jeg er naturligvis interesseret i at benytte denne function som CALLBACK, men det kan jo kun lade sig gøre med statiske funktioner.
Så hvordan får jeg i man statiske funktion fat i værdien (this) på objektet?
29. september 2001 - 14:46
#1
Her er main funktionen, som bruger en klasse, nedarvet fra pWindow klassen:
class MyWindow : public pWindow {
LRESULT Run(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) {
switch (uMsg)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
MessageBox(hwnd,\"Closing window.\",wtitle,MB_OK);
DestroyWindow(hwnd);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
};
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MyWindow wa,wb;
if (wa.open(\"Test1\",hInstance,1) && wb.open(\"Test2\",hInstance,1)) {
MSG msg;
while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0 && GetMessage(&msg, (HWND) NULL, 0, 0) != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} else {
MessageBox(NULL,\"Error, could not open window.\",\"Error\",MB_OK);
}
return 0;
}
29. september 2001 - 14:47
#2
Her er hele pWindow klassen (jeg forsøger at overføre en pointer til klassen med SetProp):
class pWindow {
HINSTANCE hinst;
public:
HWND hWnd;
char wtitle[255];
static LRESULT CALLBACK MsgHandler(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) {
char *n=(char *) GetProp(hwnd, \"WND_TITLE\");
if (n!=NULL) {
pWindow *p=(pWindow *) GetProp(hwnd, \"WND_HANDLER\");
MessageBox(NULL,\"Found window name\",n,MB_OK);
return p->Run(hwnd,uMsg,wParam,lParam);
} else {
MessageBox(NULL,\"Window name not found\",\"Error\",MB_OK);
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
virtual LRESULT Run(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)=0;
int open(char *title, HINSTANCE hinstance, int nCmdShow) {
WNDCLASS wc;
strcpy(wtitle,title);
hinst = hinstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = this->MsgHandler;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) COLOR_GRAYTEXT;
wc.lpszMenuName = NULL;
wc.lpszClassName = title;
if (!RegisterClass(&wc)) return FALSE;
hWnd = CreateWindow(
title, // name of window class
title, // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no owner window
(HMENU) NULL, // use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetProp(hWnd, \"WND_HANDLER\", this);
SetProp(hWnd, \"WND_TITLE\", title);
return TRUE;
}
};