SS); //如果采用预定义的光标,则第一参数为NULL
wndcls.hIcon=LoadIcon(NULL,IDI_WARNING);//同上
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindowProc; //回调函数
wndcls.lpszClassName="winmain"; //给本窗口类取个名字
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW; //水平和垂直重画,这在窗口水平和垂直调整大小的时候告知窗口是否需要重画,如果填写上述两个参数,则表示重画,窗口上的内容将清除重画
//第二步:
注册窗口类
RegisterClass(&;wndcls);
//第三步:创建窗口
HWND hwnd;
hwnd=CreateWindow("winmain","this is first lesson",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL, NULL,hInstance,NULL);
//第四步:显示窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
//第五步:重刷一下窗口,该语句可有可无
UpdateWindow(hwnd);
MSG msg;
//每次循环从消息队列中取出一条消息进行处理。If the GetMessage retrieves a message other than WM_QUIT, the return value is nonzero.If the function retrieves the WM_QUIT message, the return value is zero.
while(GetMessage(&;msg,NULL,0,0))
{
//该语句将类似于WM_KeyDown和WM_KeyUp转换生成新WM_CHAR投递给系统处理
//The WM_CHAR message is posted to the window with the keyboard focus
//when a WM_KEYDOWN message is translated by the Translate
Message function.
//WM_CHAR contains the character code of the key that was pressed.
TranslateMessage(&;msg);
//将消息投递出去给操作系统,操作系统会自动调用回调函数处理
DispatchMessage(&;msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
switch(uMsg) {
case WM_PAINT:
PAINTSTRUCT ps;
hdc=BeginPaint(hwnd,&;ps);
TextOut(hdc,0,10,"hello world!",strlen("hello world!"));
EndPaint(hwnd,&;ps);
break;
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"消息",MB_OK);
case WM_LBUTTONDOWN:
hdc=GetDC(hwnd); //得到当前窗口的上下文句柄
TextOut(hdc,0,50,"beijing2008!",strlen("beijing2008!"));
ReleaseDC(hwnd,hdc);
break;
case WM_CLOSE:
if(IDOK==MessageBox(hwnd,"确认要退出嘛?","消息",MB_OK))
{
//销毁窗口,但进程中还是保留的,该函数直接抛出WM_DESTROY消息
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0); //终止消息循环,并抛出WM_QUIT消息,从而导致上面的消息循环退出
default:
//对于代码中没有涉及到的消息,由提交给系统处理,此句一定要加,否则窗口不会出现
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}