#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc( /消息回调函数申明
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain( /窗口程序入口
HINSTANCE hInstance, // handle to current instance //指向当前实例
HINSTANCE hPrevInstance, // handle to previous instance//指向先前实例
LPSTR lpCmdLine, // command line //指向命令行的一段字符串
int nCmdShow // show state //窗口状态显示
)
{
WNDCLASS wndcls; ///设计窗口类
wndcls.cbClsExtra=0;//窗口类分配额外字节
wndcls.cbWndExtra=0;//窗口实例分配额外字节
wndcls.hbrBackground=(HBRUSH)GetStockObject(COLOR_SCROLLBAR);//背景设置
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;//指向回调函数
wndcls.lpszClassName="lynn";//类名
wndcls.lpszMenuName=NULL;//菜单名
wndcls.style=CS_HREDRAW | CS_VREDRAW;//实例化窗口类此处:水平重画|垂直重画
RegisterClass(&wndcls);//注册窗口类
HWND hwnd;
hwnd=CreateWindow("lynn","HELLO WORLD",WS_OVERLAPPEDWINDOW,/创建窗口
0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL); 显示窗口
UpdateWindow(hwnd); //更新窗口
MSG msg; /消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//将按键信息转化为字符信息,在把信息放入消息队列中
DispatchMessage(&msg);//将消息投递到窗口过程中,它是用在将Getmessage取回的值分发出去的典型函数
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"Programer",strlen("Programer"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"HELLO WORLD",strlen(HELLO WORLD));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
窗口程序基本过程:
1.设计窗口类
2.注册窗口类
3.创建窗口
4.显示窗口
5.消息循环