1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
class Windows_Base
{
friend LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
public:
Windows_Base(WINFO,int);
virtual ~Windows_Base(){}
//...
virtual bool On_WM_CREATE(HWND hwnd){return false;}
//...
};
class Main_Window:public Windows_Base
{
public:
Main_Window();
~Main_Window();
//...
bool On_WM_CREATE(HWND hwnd);
//...
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
Windows_Base* window = NULL;
//-----
// Process "create" message seperately in order to associate the instance of
// the BaseWindow being created with its window handle, such that it can
// be retreived when other messages are being processed
if( msg == WM_CREATE )
{
// Get the BaseWindow instance to be associated with the windows handle
// By accessing the createstruct passed in the lparam as the WM_CREATE message.
// (LPCREATESTRUCT is simply a pointer to such a structure.)
LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lparam);
// And then retreiving the pointer passed in the Create Window call.
window = reinterpret_cast<Windows_Base*>(createStruct->lpCreateParams);
if(!window)
{
throw Base_Exception(_T("Failed to get pointer to instance of BaseWindow being created"),
_T("void On_WM_CREATE(HWND hwnd,LPARAM lparam,W*& window)"),_T("Global Window Functions.cpp, Line 29."));
}
// Associate the Window instance with the windows handle
SetLastError(0);
if( !SetWindowLongPtr(hwnd, GWLP_USERDATA,reinterpret_cast<LONG_PTR>(window)))
{
DWORD err = GetLastError();
if( err )
{
std::basic_string<TCHAR> ErrMsg=_T("Could not associate BaseWindow instance with window handle./r/nGetLastError returned: ")+num2T(err);
throw Base_Exception(ErrMsg,_T("void On_WM_CREATE(HWND hwnd,LPARAM lparam,W*& window)"),_T("Global Window Functions.cpp"));
}
}
// Pass HWND to window object to undertake creation tasks
window->On_WM_CREATE(hwnd);
// Signal that the window is created and ready to go
SetEvent(window->w_created);
return 0;
}
//...
}
|