Sigh! Although the threads i read did make me understand much better of the problem, i didnt quite grasp the method of passing the "this" pointer into CreateWindow() and use SetWindowLong and GetWindowLong to redirect the message.
I tried to implement a similar method using my way, but i dont understand why it didnt work.
There are basically two problem i encountered:
Before this, you might need the header for classes
Code:
class mainFrame:public skeleton // skeleton as base class
{
public:
mainFrame(HINSTANCE hInst, UINT name, UINT wndTitle, int nCmdShow,TCHAR* str);
~mainFrame(){};
// function to register window class
virtual bool registerWindowClass(HINSTANCE hinst, UINT resPoolID);
// function to create window
virtual HWND createWindow(HINSTANCE hinst, int nCmdShow, UINT resPoolID, UINT title);
// window processing window
virtual LRESULT CALLBACK wndProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam);
private:
void mainWindow_onDestroy(HWND hwnd);
void mainWindow_onCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
};
// abstract class for any windows
class skeleton
{
public:
// 1. constructor
skeleton(HINSTANCE hInst, UINT name, UINT wndTitle, int nCmdShow, TCHAR* str);
// 2. destructor
virtual ~skeleton(){};
// 3. function to register window class
virtual bool registerWindowClass(HINSTANCE hinst, UINT resPoolID) = 0;
// 4. function to create window
virtual HWND createWindow(HINSTANCE hinst, int nCmdShow, UINT resPoolID, UINT title) = 0;
// 5. window processing window
virtual LRESULT CALLBACK wndProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam) = 0;
// 6. static window proc
static LRESULT CALLBACK staticWndproc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam);
//7. initiliaze the window
bool initInstance();
//8. set ATOM for window properties
static void setWndProcname(TCHAR* str){wndProcname = GlobalAddAtom(str);}
private:
static ATOM getWndProcname(){return wndProcname;}
static ATOM wndProcname;
HINSTANCE hInstance; // instance handle of application
UINT classname; // class name
UINT title; // title of window
int nWinMode; // default height of window
};
PROBLEM 1. why doesnt setProp,GetProp and RemoveProp accept the string parametter as an ATOM
Code:
// this is my static wndProc
LRESULT CALLBACK skeleton::staticWndproc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
// it didnt accept what getWndProc() returned,e.g.
// WNDPROC wndProc= (WNDPROC)GetProp(hwnd,getWndProcname());
// i had to hard code this to :
WNDPROC wndProc= (WNDPROC)GetProp(hwnd,"mainFrame.wndProc");
return CallWindowProc(wndProc,hwnd,message,wParam,lParam);
}
Here is what my compiler said:
C:\Documents and Settings\Edmund\My Documents\My eBooks\My expt files\windows workspace\skeleton\skeleton.cpp(40) : error C2664: 'GetPropA' : cannot convert parameter 2 from 'unsigned short' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
PROBLEM 2. I think my compiler think i gave it a void handler. Line 72 is the SetProp() function in the wndProc.
Code:
Error message:
C:\Documents and Settings\Edmund\My Documents\My eBooks\My expt files\windows workspace\skeleton\mainFrame.cpp(72) : error C2440: 'type cast' : cannot convert from 'long (__stdcall mainFrame::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'v
oid *'
Code:
// function to create window
HWND mainFrame::createWindow(HINSTANCE hinst, int nCmdShow, UINT resPoolID, UINT title)
{
HWND hwnd; // handler for window to be created
TCHAR ClassName[MAX_RESOURCESTRING+1]; // window class name
TCHAR wndTitle[MAX_RESOURCESTRING+1]; // window title
// load class name into ClassName
LoadString(hinst,resPoolID,ClassName,DIM(ClassName));
// load title name into title
LoadString(hinst,title,wndTitle,DIM(wndTitle));
hwnd = CreateWindowEx(0, // extended window style
ClassName, // class name
wndTitle, // title
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // default x-pos of window
0, // y-pos of window, set 0 and use showWindow() to determine later
400, // width of window
400, // height of window
NULL, // parent of window
LoadMenu(hinst, MAKEINTRESOURCE(resPoolID)), // handle of menu
hinst, //handle of application
NULL); // user defined parameters, hardly any use for it
if(!hwnd)
return NULL;
// add wndProc handle to window properties
SetProp(hwnd,"mainFrame.wndProc",(HANDLE)wndProc);
// make window appear
ShowWindow(hwnd,nCmdShow);
// force window to update
UpdateWindow(hwnd);
return hwnd;
}