#include<windows.h>
//=============================
//HINSTANCE hInst;
HINSTANCE hInstance;
char lpszClassName[] = "classclassdf";
char *ShowText;
MSG msg;
//=============================
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void On_LButtonDown(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
void On_Paint(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
void On_Destroy(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
//=============================
class CFrameWnd{
public:
HWND hWnd;
public:
int RegisterWindow();
void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName);
void ShowWindow(int nCmdShow);
void UpdateWindow();
};
int CFrameWnd::RegisterWindow(){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.style = 0;
wc.hInstance = hInstance;
wc.lpszClassName = lpszClassName;
wc.lpszMenuName = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc = WndProc;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
return RegisterClass(&wc);
}
void CFrameWnd::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName){
RegisterWindow();
// hInst = hInstance;
hWnd = CreateWindow(lpszClassName,
lpWindowName,
WS_OVERLAPPEDWINDOW,
0,0,800,600,
NULL,
NULL,
hInstance,
NULL);
}
void CFrameWnd::ShowWindow(int nCmdWnd){
::ShowWindow(hWnd,nCmdWnd);
}
void CFrameWnd::UpdateWindow(){
::UpdateWindow(hWnd);
}
//=============================================================
class CWinApp{
public:
CWinApp *m_pCurrentWinApp;
public:
CWinApp();
~CWinApp();
public:
CFrameWnd *m_pMainWnd;
public:
virtual BOOL InitInstance(int nCmdShow);
int Run();
};
CWinApp::CWinApp(){
m_pCurrentWinApp = this;
}
BOOL CWinApp::InitInstance(int nCmdShow){
m_pMainWnd = new CFrameWnd;
m_pMainWnd->Create(NULL,"封装的Windows程序");
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CWinApp::Run(){
while( GetMessage(&msg, NULL, NULL, NULL) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
CWinApp::~CWinApp(){
delete m_pMainWnd;
}
//==================================================
class CMyWnd:public CFrameWnd{
};
//==================================================
class CMyApp:public CWinApp{
public:
BOOL InitInstance(int nCmdShow);
};
//==================================================
BOOL CMyApp::InitInstance(int nCmdShow){
CMyWnd * pMainWnd;
pMainWnd = new CMyWnd;
pMainWnd->Create(NULL,"应用窗体的派生类的程序");
pMainWnd->ShowWindow(nCmdShow);
pMainWnd->UpdateWindow();
m_pMainWnd = pMainWnd;
return TRUE;
}
//==================================================
CWinApp MyApp;
//==================================================
CWinApp *AfxGetApp(){
return MyApp.m_pCurrentWinApp;
}
//==================================================
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int ResultCode = -1;
CWinApp *pApp;
pApp = AfxGetApp();
pApp->InitInstance(SW_SHOW);
return ResultCode = pApp->Run();
}
//==================================================
LRESULT CALLBACK WndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message){
case WM_LBUTTONDOWN:
On_LButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
On_Paint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
On_Destroy(hWnd,message,wParam,lParam);
default:
DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void On_LButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
ShowText = "Hello!";
InvalidateRect(hWnd,NULL,1);
}
void On_Paint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 50, 50, ShowText, 6);
EndPaint(hWnd,&ps);
}
void On_Destroy(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
PostQuitMessage(0);
}