Hello!
I wanted to write a simple classe for Window Management (W32 API).
My idea was to write a base class (CWindow) and overwrite the WindowProcedure it in a Child class (MyWindow) .
Here is the code (long):
int CWindow::Create(HINSTANCE instance,int aFunsterStil,WNDCLASSEX wincl,int width, int height)
{
FunsterStil=aFunsterStil;
// Register the window class
if(!RegisterClassEx (&wincl))
return 0;
// Create the window
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
classname, /* Classname */
caption, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
width, /* The programs width */
height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
instance, /* Program Instance handler */
NULL /* No Window Creation data */
);
// Make the window visible on the screen
ShowWindow (hwnd, FunsterStil);
}
int CWindow::Create(HINSTANCE instance,int aFunsterStil,int width,int height,UINT style,LPCTSTR menuName,int clsExtra, int wndExtra)
{
FunsterStil=aFunsterStil;
// The Window structure
wincl.hInstance = instance;
wincl.lpszClassName = classname;
wincl.lpfnWndProc = WindowProc; /* This function is called by windows */
wincl.style = style; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = menuName; /* No menu */
wincl.cbClsExtra = clsExtra; /* No extra bytes after the window class */
wincl.cbWndExtra = wndExtra; /* structure or the window instance */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
// Register the window class
if(!RegisterClassEx (&wincl))
return 0;
// Create the window
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
classname, /* Classname */
caption, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
width, /* The programs width */
height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
instance, /* Program Instance handler */
NULL /* No Window Creation data */
);
// Make the window visible on the screen
ShowWindow (hwnd, FunsterStil);
}
LRESULT CALLBACK MyWindow::WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CLOSE:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
CWindow::WindowProc(hwnd,message,wParam,lParam);
return DefWindowProc (hwnd, message, wParam, lParam);
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
TheApp MyApp(hThisInstance,nFunsterStil);
MSG messages;
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
The Problem is, that in the derived class, WindowProc is never called. I thought that if I would overwrite the method, it would be used in Create(...), but instead CWindow::WindowProc is used.
I don't want to rewrite the whole Create(..) method in the derived class as this would mean nearly no work saved with it :(((.
Please excuse my bad english, thanks in advance :D
Greetings,
Paul
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your problem seems to be the standard OOP callback problem. I don't think you can get it working without providing a pointer to 'this' to your static callback. About 2 weeks ago I put my dialog box class skeleton on this forum. You just substitute dialogbox stuff with window stuff and and see how a callback can be encapsulated.
Or search google for 'static callback'.
rpeter
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello!
I wanted to write a simple classe for Window Management (W32 API).
My idea was to write a base class (CWindow) and overwrite the WindowProcedure it in a Child class (MyWindow) .
Here is the code (long):
MYWND.H
----------------
#ifndef __MYWND_H__
#define __MYWND_H__
#include <windows.h>
class CWindow
{
private:
char *classname,*caption;
HWND hwnd;
WNDCLASSEX wincl;
int FunsterStil;
public:
static LRESULT CALLBACK WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
CWindow(char* aclassname,char* acaption);
int Create(HINSTANCE instance,int aFunsterStil,WNDCLASSEX wincl,int width=544,int height=475);
int Create(HINSTANCE instance,int aFunsterStil,int width=544,
int height=475,UINT style=CS_DBLCLKS,LPCSTR menuName=NULL,
int clsExtra=0,int wndExtra=0);
void Show();
void Hide();
~CWindow();
};
#endif
MYWND.CPP
---------------------
#include <windows.h>
#include <strings.h>
#include "MyWnd.h"
CWindow::CWindow(char* aname,char* acaption)
{
classname=new char[strlen(aname)];
caption=new char[strlen(acaption)];
strcpy(classname,aname);
strcpy(caption,acaption);
}
CWindow::~CWindow()
{
delete [] classname;
delete [] caption;
}
LRESULT CALLBACK CWindow::WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc (hwnd, message, wParam, lParam);
}
int CWindow::Create(HINSTANCE instance,int aFunsterStil,WNDCLASSEX wincl,int width, int height)
{
FunsterStil=aFunsterStil;
// Register the window class
if(!RegisterClassEx (&wincl))
return 0;
// Create the window
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
classname, /* Classname */
caption, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
width, /* The programs width */
height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
instance, /* Program Instance handler */
NULL /* No Window Creation data */
);
// Make the window visible on the screen
ShowWindow (hwnd, FunsterStil);
}
int CWindow::Create(HINSTANCE instance,int aFunsterStil,int width,int height,UINT style,LPCTSTR menuName,int clsExtra, int wndExtra)
{
FunsterStil=aFunsterStil;
// The Window structure
wincl.hInstance = instance;
wincl.lpszClassName = classname;
wincl.lpfnWndProc = WindowProc; /* This function is called by windows */
wincl.style = style; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = menuName; /* No menu */
wincl.cbClsExtra = clsExtra; /* No extra bytes after the window class */
wincl.cbWndExtra = wndExtra; /* structure or the window instance */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
// Register the window class
if(!RegisterClassEx (&wincl))
return 0;
// Create the window
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
classname, /* Classname */
caption, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
width, /* The programs width */
height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
instance, /* Program Instance handler */
NULL /* No Window Creation data */
);
// Make the window visible on the screen
ShowWindow (hwnd, FunsterStil);
}
void CWindow::Show()
{
ShowWindow (hwnd, FunsterStil);
}
void CWindow::Hide()
{
ShowWindow (hwnd,SW_HIDE);
}
MAIN.H
-----------
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#include "MyWnd.h"
class TheApp
{
private:
public:
TheApp(HINSTANCE instance,int FunsterStil);
};
class MyWindow: public CWindow
{
private:
static LRESULT CALLBACK WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
public:
MyWindow(char* aclassname,char* acaption);
};
#endif
MAIN.CPP
----------------
#include <windows.h>
#include "main.h"
TheApp::TheApp(HINSTANCE instance, int FunsterStil)
{
MyWindow Window1("Window1","Window1");
MyWindow Window2("Window2","Window2");
Window1.Create(instance,FunsterStil,555,666);
Window2.Create(instance,FunsterStil,777,333);
}
MyWindow::MyWindow(char* aclassname,char* acaption):CWindow(aclassname,acaption) {} // Konstruktorschreibweise
LRESULT CALLBACK MyWindow::WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CLOSE:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
CWindow::WindowProc(hwnd,message,wParam,lParam);
return DefWindowProc (hwnd, message, wParam, lParam);
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
TheApp MyApp(hThisInstance,nFunsterStil);
MSG messages;
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
The Problem is, that in the derived class, WindowProc is never called. I thought that if I would overwrite the method, it would be used in Create(...), but instead CWindow::WindowProc is used.
I don't want to rewrite the whole Create(..) method in the derived class as this would mean nearly no work saved with it :(((.
Please excuse my bad english, thanks in advance :D
Greetings,
Paul
omg, sf delete all the spaces !
Sorry for that. Is there a way to get around this ?
Paul
Your problem seems to be the standard OOP callback problem. I don't think you can get it working without providing a pointer to 'this' to your static callback. About 2 weeks ago I put my dialog box class skeleton on this forum. You just substitute dialogbox stuff with window stuff and and see how a callback can be encapsulated.
Or search google for 'static callback'.
rpeter