About
I am trying to make an alternative to the
MFC
, using the
Win32 API
. Which is I am going to wrap it into
classes
. My first approach was not succeeded.
The Whole Code
#include <Windows.h>
#include <tchar.h>
class CWnd
{
public:
virtual void Init(HINSTANCE hInstance) {
HWND hwnd ;
WNDCLASS wndclass ;
wndclass.style = 0 ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = L"CWndMy32" ;
RegisterClass (&wndclass);
hwnd = CreateWindow (L"CWndMy32", TEXT ("The Hello Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,
(LPVOID)this) ;
ShowWindow (hwnd, SW_SHOWNORMAL) ;
UpdateWindow (hwnd) ;
}
protected:
HINSTANCE hInst;
HWND m_hwnd;
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CWnd * _this = (CWnd *) GetWindowLong(hwnd, GWL_USERDATA);
if(msg == WM_NCCREATE)
{
_this = new CWnd;
_this->hInst = GetModuleHandle(0);
_this->m_hwnd = hwnd;
SetWindowLong(hwnd, GWL_USERDATA, (LONG) _this);
return TRUE;
}
return _this->VirtualProc(hwnd, msg, wParam, lParam);
}
virtual LRESULT VirtualProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam)
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
};
class CMyWnd : public CWnd
{
private:
LRESULT VirtualProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_LBUTTONDOWN)
MessageBox(0, 0, 0, 0); return DefWindowProc(hwnd, msg, wParam, lParam);
}
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
CMyWnd wnd; wnd.Init(hInstance);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
What I Did
1. Created a
Base
Class Called '
CWnd
' (also I call it the
TEMPLATE CLASS
)
2. In
CWnd::Init
;Registered, Created and Showed the window.
(In the CreateWindow call, I Attached a
pointer
of the
CWnd
to
the window, So that every derived class of
CWnd
will have different data,
but a same
static
window procedure )
3.
CWnd::WndProc
is the
Static
window procedure.
4.
CWnd::VirtualProc
is the
Virtual
window procedure which is overridden in derived
classes.
5. Created a Derived class using
TEMPLATE CLASS
, called '
CMyWnd
'.
6.
CMyWnd::VirtualProc
is the function that overrode
CWnd::VirtualProc
.
The Problem
Code can be compiled with no errors, and the window is created nicely. But the problem is that,
CWnd::VirtualProc
virtual function is not correctly overrode with
CMyWnd::VirtualProc
.
Any Help ?? Please ?