#include <windows.h>
#include <windowsx.h>
//函数声明
BOOL InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WinProc ( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
//Win32程序入口函数,创建主窗口,处理消息循环
int PASCAL WinMain( HINSTANCE hInstance, //当前句柄实例
HINSTANCE hPrevInstance, //前一个句柄实例
LPSTR lpCmdLine, //命令行字符
int nCmdShow) //窗口显示方式
{
MSG msg;
//创建主窗口
if (!InitWindow(hInstance,nCmdShow)){
return FALSE;
}
//进入消息循环:
//从该应用程序的消息队列中检取消息,送到消息处理过程,
//当检取到WM_QUIT消息时,退出消息循环
while (GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//结束
return msg.wParam;
}
//创建窗口
static BOOL InitWindow(HINSTANCE hInstance,int nCmdShow){
HWND hwnd; //窗口句柄
WNDCLASS wc; //窗口类结构
//填充窗口类结构
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wc.hCursor = LoadCursor(hInstance,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "LostAngel";
RegisterClass(&wc); //注册窗口类
hwnd = CreateWindow( //创建主窗口
"MyGame", //窗口类名称
"Welcome to my window!", //窗口名称
WS_OVERLAPPEDWINDOW, //窗口风格
100, //窗口位置的X坐标
100, //窗口位置的Y坐标
400, //窗口位置的宽度
300, //窗口位置的高度
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //应用程序实例句柄
NULL); //窗口创建数据指针
if (!hwnd){
return FALSE;
}
//显示并更新窗口
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
//处理主窗口消息
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_KEYDOWN: //击键消息
switch(wParam){
case VK_ESCAPE:MessageBox(hWnd,"Esc键被按下!","Keyboard",MB_OK);
break;
}
break;
case WM_RBUTTONDOWN: //鼠标右键
{
MessageBox(hWnd,"鼠标右键被按下","Mouse",MB_OK);
break;
}
case WM_PAINT: //窗口重画消息
{
char hello[] = "你好,欢迎使用MyGame!";
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd,&ps); //取得设备环境句柄
SetTextColor(hdc,RGB(0,0,255)); //设置文字颜色
TextOut(hdc,20,10,hello,strlen(hello)); //输出文字
EndPaint(hWnd,&ps); //释放资源
break;
}
case WM_DESTROY: //退出消息
PostQuitMessage(0); //调用退出函数
break;
}
//调用缺省消息处理过程
return DefWindowProc(hWnd,message,wParam,lParam);
}
本代码在VS2008下编写,编译通过。