标签:
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE hInst; /* The ‘main‘ function of Win32 GUI programs: this is where execution starts */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口类名*/ HWND hwnd; /*窗口句柄*/ MSG msg; /*消息*/ WNDCLASS wndclass; /*窗口类*/ hInst = hInstance; /***********第一步:注册窗口类*************/ /*为窗口类各个字段赋值*/ wndclass.style = CS_HREDRAW | CS_VREDRAW; /*窗口风格*/ wndclass.lpfnWndProc = WndProc; /*窗口过程*/ wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; /*当前窗口句柄*/ wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); /*窗口图标*/ wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);/*鼠标样式*/ wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);/*窗口背景画刷*/ wndclass.lpszMenuName = NULL; /*窗口菜单*/ wndclass.lpszClassName= szClassName; /*窗口类名*/ /*注册窗口*/ RegisterClass(&wndclass); /*************第二步:创建窗口(并让窗口显示出来)***************/ hwnd = CreateWindow( szClassName, /*窗口名字*/ TEXT("Welcome"), /*窗口标题*/ WS_OVERLAPPEDWINDOW, /*窗口风格*/ CW_USEDEFAULT, /*初始化x轴的位置*/ CW_USEDEFAULT, /*初始化y轴的位置*/ 640, /*窗口宽度*/ 480, /*窗口高度*/ NULL, /*父窗口句柄*/ NULL, /*窗口菜单句柄*/ hInstance, /*当前窗口句柄*/ NULL /*不使用该值*/ ); if(hwnd == NULL) { MessageBox(NULL, "创建窗口出错!", "Error", MB_OK); return -1; } /*显示窗口*/ ShowWindow(hwnd, nCmdShow); /*更新(绘制)窗口*/ UpdateWindow(hwnd); /*************第三步:消息循环*************/ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); /*翻译消息*/ DispatchMessage(&msg); /*分派消息*/ } return msg.wParam; /*当GetMessage程序返回FALSE是程序结束*/ } /*************第四步:窗口过程*****************/ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; //必须设置为静态变量 static int iClick = 0; //鼠标单击次数 static TCHAR szTextBuf[20]; //static 控件文本 static HWND hStatic; // static 控件句柄 switch(message) { case WM_CREATE: { hStatic = CreateWindow( "static", //静态文本框的类名 "请用鼠标单击", //控件文本 WS_CHILD /*子窗口*/| WS_VISIBLE /*创建时显示*/| WS_BORDER /*带边框*/| SS_CENTER /*水平居中*/| SS_CENTERIMAGE/*垂直居中*/, 20 /*X坐标*/, 20 /*Y坐标*/, 200 /*宽度*/, 100 /*高的*/, hWnd, // 父窗口句柄 (HMENU)1, //为控件指定唯一标示符 hInst, //当前程序实例句柄 NULL ); break; } case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); //Ellipse(hdc, 0, 100, 200, 200); EndPaint(hWnd, &ps); break; } //鼠标左键单击消息 case WM_LBUTTONDOWN: { iClick++; wsprintf(szTextBuf, TEXT("鼠标被单击%d次"), iClick); SetWindowText(hStatic, szTextBuf); break; } /*窗口销毁消息*/ case WM_DESTROY: { /*处理WM_DESTROY消息时删除之前我们创建的一切GDI对象*/ PostQuitMessage(0); break; } default: { return DefWindowProc(hWnd, message, wParam, lParam); } } return 0; }
标签:
原文地址:http://my.oschina.net/u/241930/blog/527565