#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) { int wmId, wmEvent; HDC hdc; PAINTSTRUCT ps; static HFONT hFont; //声明一个逻辑字体句柄 static HWND hBtn; // 声明一个按钮控件句柄 static HWND hStatic; // 声明静态文本控件句柄 switch(message) { case WM_CREATE: { //创建逻辑字体 hFont = CreateFont( -20/*高度*/, -10/*宽度*/,0,0,400/*一般设值为400*/, FALSE/*不带斜体*/, FALSE/*不带下划线*/,FALSE/*不带删除线*/, DEFAULT_CHARSET, //使用默认字符集 OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, //默认输出质量 FF_DONTCARE, //不指定字体族 TEXT("微软雅黑") //字体名 ); //创建文本控件 hStatic = CreateWindow( TEXT("static"), //静态文本框的类名 TEXT("Hello, Welcome to C world!"), //控件文本 WS_CHILD | WS_VISIBLE/*创建时显示*/ | WS_BORDER/*带边框*/, 30, 20, 150, 80,//X坐标,Y坐标,宽度和高度 hWnd, (HMENU)1, hInst, NULL ); //创建按钮控件 hBtn = CreateWindow( TEXT("Button"), //按钮控件类名 TEXT("点击按钮试试"), WS_CHILD | WS_VISIBLE | BS_FLAT/*扁平样式*/, 30/*X坐标*/,110/*Y坐标*/, 150/*宽度*/,50/*高度*/, hWnd, (HMENU)2/*控件唯一标识符*/, hInst, NULL ); //设置按钮字体 SendMessage(hStatic, WM_SETFONT, (WPARAM)hFont, NULL);//设置文本框字体 SendMessage(hBtn, WM_SETFONT, (WPARAM)hFont, NULL);//设置按钮字体 break; } case WM_COMMAND: { wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch(wmId) { case 2: //按下按钮 { //更改文本框的内容 SetWindowText(hStatic, TEXT("你点击了下面的按钮")); break; } default: { //不处理的消息一定要交给defWindowProc处理 return DefWindowProc(hWnd, message, wParam, lParam); break; } } break; } case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); //Ellipse(hdc, 0, 100, 200, 200); EndPaint(hWnd, &ps); break; } /*窗口销毁消息*/ case WM_DESTROY: { /*处理WM_DESTROY消息时删除之前我们创建的一切GDI对象*/ DeleteObject(hFont); //删除创建的字体 PostQuitMessage(0); break; } default: { return DefWindowProc(hWnd, message, wParam, lParam); } } return 0; }