方法一:
File> New >MFCWizard
方法二(很长):
#include
LRESULT CALLBACK WinSunProc (HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam); //回调函数的前置声明
//程序入口点
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nShowCmd)
{
//设计窗口类(WNDCLASS)
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WinSunProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,NULL);
wndclass.hCursor = LoadCursor(NULL,NULL);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_PEN);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = TEXT("A");
//注册窗口(RegisterClass)
RegisterClassA(&wndclass);
//生成窗口(CreateWindow)
HWND hwnd;
hwnd = CreateWindow(TEXT("A"),TEXT("A"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,1024,768,
NULL,NULL,
hInstance,
NULL);
//显示窗口(ShowWindow)
ShowWindow(hwnd,SW_SHOWNORMAL);
//更新窗口(UpdateWindow)
UpdateWindow(hwnd);
//消息循环(GetMessage)
MSG msg;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//回调函数(LRESULT CALLBACK)
LRESULT CALLBACK WinSunProc (HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
MessageBox(hwnd,TEXT("Hello World!"),TEXT("A"),MB_OK);
}
break;
case WM_CLOSE:
{
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
用MSDN逐个api函数慢慢对着看,就会明白的,
就是6个步骤:
1.设计窗口
(WNDCLASS)
2.注册窗口
(RegisterClass)
3.创建窗口
(CreateWindow)
4.显示窗口
(ShowWindow)
5.更新窗口
(UpdateWindow)
6.消息循环
这就是窗口编写最重要的地方了,
回调函数:一个筛选窗口消息的函数,叫做回调函数,它由操作系统来调用
取消
评论