过程如下:
(1)设计窗体类
(2)注册窗体类
(3)创建窗体
(4)显示窗体和更新窗体
(5)消息循环
(6)窗口过程(又称回调函数)
代码如下:
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunPro( //declare the function of WinSumPro
HWNDhwnd,
UINTuMsg,
WPARAMwParam,
LPARAMlParam
);
int WINAPI WinMain(
HINSTANCEhInstance, //handle to current instance
HINSTANCEhPrevInstance, //handle to previousinstance
LPSTRlpCmdLine, //command line
intnCmdshow //show state
)
{
WNDCLASSwndcls; //1、desingedwindow class
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_HELP);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunPro;
wndcls.lpszClassName="Sunlei2015";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls); //2、register windowclass
HWNDhwnd; //3、createwindow
hwnd=CreateWindow("Sunlei2015","feiyan",WS_OVERLAPPEDWINDOW,400,200,600,300,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_NORMAL); //4、show window
UpdateWindow(hwnd); //5、updatewindow
MSGmsg; //6、messagecircumstance
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg); //将消息分配给窗口过程来处理
}
return0;
}
LRESULT CALLBACK WinSunPro( //7、window process
HWNDhwnd,
UINTuMsg,
WPARAMwParam,
LPARAMlParam
)
{
switch(uMsg) //handle themessage by message's type
{
caseWM_CHAR:
charszChar[20];
sprintf(szChar,"charis %d",wParam);
MessageBox(hwnd,szChar,"Sunlei2015",0);
break;
caseWM_LBUTTONDOWN:
MessageBox(hwnd,"mouseclick","Sunlei2015",0);
HDChdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"Mouseclick seccessfully!",strlen("Mouse click seccessfully!"));
ReleaseDC(hwnd,hdc);
break;
caseWM_PAINT:
HDChDC;
PAINTSTRUCTps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"C++programming ",strlen("C++ programming "));
EndPaint(hwnd,&ps);
break;
caseWM_CLOSE:
if(IDYES==MessageBox(hwnd,"真的要关闭吗?","Sunlei2015",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
caseWM_DESTROY:
PostQuitMessage(0);
break;
default:
returnDefWindowProc(hwnd,uMsg,wParam,lParam);
}
return0;
}