#include
HINSTANCE hInst;
HINSTANCE hInstance;
MSG msg;
char lpszClassName[]="window_class";
char* ShowText;
//TCHAR *ShowText;
//声明函数原型
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//窗口函数
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnPaint (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnDestroy (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.style=0;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=lpszClassName;
return RegisterClass(&wc);
}
void CFrameWnd::Create(LPCTSTR lpszClassName,LPCTSTR lpWindowName)
{
RegisterWindow();
hInst=hInstance;
hWnd=CreateWindow(lpszClassName,lpWindowName,WS_OVERLAPPEDWINDOW,200,200,200,200,NULL,NULL,hInstance,NULL);
}
void CFrameWnd::ShowWindow(int nCmdShow)
{
::ShowWindow(hWnd,nCmdShow);
}
void CFrameWnd::UpdateWindow()
{
::UpdateWindow(hWnd);
}
//声明应用程序类
class CWinApp
{
public:
CFrameWnd *m_pMainWnd;
public:
BOOL InitInstance(int nCmdShow);
int Run();
~CWinApp();
};
//应用程序类成员函数的实现
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,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
CWinApp::~CWinApp(){delete m_pMainWnd;}
//程序员定义的WinApp对象theApp-------------------------------------------------------------------
CWinApp theApp;
//主函数-----------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,LPSTR lpCmdLine,int nCmdShow)
{
int ResultCode=-1;
theApp.InitInstance(nCmdShow);
return ResultCode=theApp.Run();
}
//窗口函数的实现
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
OnPaint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
OnDestroy(hWnd,message,wParam,lParam);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
ShowText="Hello!";
InvalidateRect(hWnd,NULL,1);
}
void OnPaint(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 OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
}