标签:
#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"); /*窗口类名*/ 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)GetSysColorBrush(COLOR_BTNFACE);/*窗口背景画刷(灰色)*/ wndclass.lpszMenuName = NULL; /*窗口菜单*/ wndclass.lpszClassName= szClassName; /*窗口类名*/ /*注册窗口*/ RegisterClass(&wndclass); /*************第二步:创建窗口(并让窗口显示出来)***************/ HWND 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); /*************第三步:消息循环*************/ MSG msg; 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; PAINTSTRUCT ps; HDC hdc; static HFONT hFont; // 逻辑字体 //一组单选按钮 static HWND hGroupSex; //静态文本框--性别 static HWND radioMale; //单选按钮--男 static HWND radioFemale; //单选按钮--女 //一组单选按钮 static HWND hGroupMarriage; //静态文本框--婚姻状况 static HWND radioMarried; //单选按钮--已婚 static HWND radioSingle; //单选按钮--未婚 static HWND radioSecrecy; //单选按钮--保密 //一组复选框 static HWND hGroupPet; //静态文本框--你的宠物 static HWND checkboxDog;//复选框--狗 static HWND checkboxCat;//复选框--猫 static HWND checkboxFish;//复选框--鱼 static HWND checkboxOther;//复选框--其他 //设置缓冲区 static TCHAR szBufSex[10]; //保存性别 static TCHAR szBufMarriage[10]; //保存婚姻状况 static TCHAR szBufPet[20]; //保存动物 static TCHAR szBufSubmit[100]; //保存提交时显示的文本 static TCHAR szBufTmp[10]; //临时缓冲区 static HWND btnSubmit; //按钮控件 switch(message) { case WM_CREATE: { //创建逻辑字体 hFont = CreateFont( -14/*高度*/, -7/*宽度*/,0,0,400/*一般设值为400*/, FALSE/*不带斜体*/, FALSE/*不带下划线*/,FALSE/*不带删除线*/, DEFAULT_CHARSET, //使用默认字符集 OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, //默认输出质量 FF_DONTCARE, //不指定字体族 TEXT("微软雅黑") //字体名 ); //创建静态文本框控件--选择性别 hGroupSex = CreateWindow( TEXT("static"), TEXT("你的性别:"), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT, 10, 10, 80, 26, hWnd, (HMENU)1,/*控件ID*/ hInst, NULL ); radioMale = CreateWindow( TEXT("button"), TEXT("男"), WS_CHILD | WS_VISIBLE | BS_LEFT/*文字居左*/ | BS_AUTORADIOBUTTON/*单选按钮*/ | WS_GROUP, 95, 10, 50, 26, hWnd, (HMENU)2, hInst, NULL ); radioFemale = CreateWindow( TEXT("button"), TEXT("女"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON, 150, 10, 50, 26, hWnd, (HMENU)2, hInst, NULL ); //选择婚姻状况 hGroupMarriage = CreateWindow( TEXT("static"), TEXT("婚姻状况:"), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT, 10, 40, 80, 26, hWnd, (HMENU)4, hInst, NULL ); radioMarried = CreateWindow( TEXT("button"), //按钮控件类名 TEXT("已婚"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON | WS_GROUP, 95, 40, 65, 26, hWnd, (HMENU)5, hInst, NULL ); radioSingle = CreateWindow( TEXT("button"), TEXT("未婚"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON, 165, 40, 65, 26, hWnd, (HMENU)6, hInst, NULL ); radioSecrecy = CreateWindow( TEXT("button"), TEXT("保密"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON, 235, 40, 100, 26, hWnd, (HMENU)7, hInst, NULL ); //你的宠物 hGroupPet = CreateWindow( TEXT("static"), TEXT("你的宠物:"), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT, 10, 70, 80, 26, hWnd, (HMENU)8, hInst, NULL ); checkboxDog = CreateWindow( TEXT("button"), TEXT("狗"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX/*复选框*/, 95, 70, 50, 26, hWnd, (HMENU)9, hInst, NULL ); checkboxCat = CreateWindow( TEXT("button"), TEXT("猫"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX, 150, 70, 50, 26, hWnd, (HMENU)10, hInst, NULL ); checkboxFish = CreateWindow( TEXT("button"), TEXT("鱼"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX, 205, 70, 50, 26, hWnd, (HMENU)11, hInst, NULL ); checkboxOther = CreateWindow( TEXT("button"), TEXT("其他"), WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX, 260, 70, 65, 26, hWnd, (HMENU)12, hInst, NULL ); //创建按钮控件 btnSubmit = CreateWindow( TEXT("button"), TEXT("提交"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_FLAT, 95, 110, 200, 36, hWnd, (HMENU)13, hInst, NULL ); //依次设置控件字体 SendMessage(hGroupSex, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(radioMale, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(radioFemale, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(hGroupMarriage, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(radioMarried, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(radioSingle, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(radioSecrecy, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(hGroupPet, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(checkboxDog, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(checkboxCat, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(checkboxFish, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(checkboxOther, WM_SETFONT, (WPARAM)hFont, 0); SendMessage(btnSubmit, WM_SETFONT, (WPARAM)hFont, 0); break; } //这里是重点 case WM_COMMAND: { wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); if(wmEvent == BN_CLICKED) { switch(wmId) { //获取性别 case 2: case 3: { GetWindowText((HWND)lParam, szBufSex, 10); break; } //获取婚姻状况 case 5: case 6: case 7: { GetWindowText((HWND)lParam, szBufMarriage, 10); } case 9: case 10: case 11: case 12: { memset(szBufPet, 0, sizeof(szBufPet)); //是否选中狗 if(SendMessage(checkboxDog, BM_GETCHECK, 0, 0) == BST_CHECKED) { GetWindowText(checkboxDog, szBufTmp, 10); wsprintf(szBufPet, TEXT("%s, %s"), szBufPet, szBufTmp); } //是否选中猫 if(SendMessage(checkboxCat, BM_GETCHECK, 0, 0) == BST_CHECKED) { GetWindowText(checkboxCat, szBufTmp, 10); wsprintf(szBufPet, TEXT("%s, %s"), szBufPet, szBufTmp); } //是否选中鱼 if(SendMessage(checkboxFish, BM_GETCHECK, 0, 0) == BST_CHECKED) { GetWindowText(checkboxFish, szBufTmp, 10); wsprintf(szBufPet, TEXT("%s, %s"), szBufPet, szBufTmp); } //是否选中其他 if(SendMessage(checkboxOther, BM_GETCHECK, 0, 0) == BST_CHECKED) { GetWindowText(checkboxOther, szBufTmp, 10); wsprintf(szBufPet, TEXT("%s, %s"), szBufPet, szBufTmp); } break; } //提交按钮 case 13: { wsprintf(szBufSubmit, TEXT("你的性别:%s\n婚姻状况:%s\r\n你的宠物:%s"), szBufSex, szBufMarriage, szBufPet); MessageBox(hWnd, szBufSubmit, TEXT("信息提示"), MB_ICONINFORMATION); break; } default: { return DefWindowProc(hWnd, message, wParam, lParam); } } } } 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; }
标签:
原文地址:http://my.oschina.net/u/241930/blog/528694