1 // 01控件.cpp : 定义应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "01控件.h" 6 #include "Windowsx.h" 7 #include8 #define MAX_LOADSTRING 100 9 10 // 全局变量: 11 HINSTANCE hInst; // 当前实例 12 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 13 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 14 15 // 此代码模块中包含的函数的前向声明: 16 ATOM MyRegisterClass(HINSTANCE hInstance); 17 BOOL InitInstance(HINSTANCE, int); 18 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 19 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 20 21 int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 22 _In_opt_ HINSTANCE hPrevInstance, 23 _In_ LPTSTR lpCmdLine, 24 _In_ int nCmdShow) 25 { 26 UNREFERENCED_PARAMETER(hPrevInstance); 27 UNREFERENCED_PARAMETER(lpCmdLine); 28 29 // TODO: 在此放置代码。 30 MSG msg; 31 HACCEL hAccelTable; 32 33 // 初始化全局字符串 34 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 35 LoadString(hInstance, IDC_MY01, szWindowClass, MAX_LOADSTRING); 36 MyRegisterClass(hInstance); 37 38 // 执行应用程序初始化: 39 if (!InitInstance (hInstance, nCmdShow)) 40 { 41 return FALSE; 42 } 43 44 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY01)); 45 46 // 主消息循环: 47 while (GetMessage(&msg, NULL, 0, 0)) 48 { 49 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 50 { 51 TranslateMessage(&msg); 52 DispatchMessage(&msg); 53 } 54 } 55 56 return (int) msg.wParam; 57 } 58 59 60 61 // 62 // 函数: MyRegisterClass() 63 // 64 // 目的: 注册窗口类。 65 // 66 ATOM MyRegisterClass(HINSTANCE hInstance) 67 { 68 WNDCLASSEX wcex; 69 70 wcex.cbSize = sizeof(WNDCLASSEX); 71 72 wcex.style = CS_HREDRAW | CS_VREDRAW; 73 wcex.lpfnWndProc = WndProc; 74 wcex.cbClsExtra = 0; 75 wcex.cbWndExtra = 0; 76 wcex.hInstance = hInstance; 77 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY01)); 78 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 79 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 80 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY01); 81 wcex.lpszClassName = szWindowClass; 82 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 83 84 return RegisterClassEx(&wcex); 85 } 86 87 // 88 // 函数: InitInstance(HINSTANCE, int) 89 // 90 // 目的: 保存实例句柄并创建主窗口 91 // 92 // 注释: 93 // 94 // 在此函数中,我们在全局变量中保存实例句柄并 95 // 创建和显示主程序窗口。 96 // 97 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 98 { 99 HWND hWnd;100 101 hInst = hInstance; // 将实例句柄存储在全局变量中102 103 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,104 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);105 106 if (!hWnd)107 {108 return FALSE;109 }110 111 ShowWindow(hWnd, nCmdShow);112 UpdateWindow(hWnd);113 114 return TRUE;115 }116 117 LRESULT CALLBACK DlgProc(118 HWND hWnd, 119 UINT message, 120 WPARAM wParam, 121 LPARAM lParam)122 {123 switch (message)124 {125 case WM_INITDIALOG:126 {127 HWND hCom = GetDlgItem(hWnd, IDC_COMBO1);128 ComboBox_AddString(hCom, TEXT("北京"));129 ComboBox_AddString(hCom, TEXT("上海"));130 ComboBox_AddString(hCom, TEXT("天津"));131 ComboBox_AddString(hCom, TEXT("南京"));132 }133 break;134 135 136 137 case WM_HSCROLL:138 {139 140 HWND hProce = GetDlgItem(hWnd, IDC_SLIDER1);141 int nNum = SendMessage(hProce, TBM_GETPOS, 0, 0);142 TCHAR buf[10] = {};143 _stprintf_s(buf, TEXT("%d"), nNum);144 SetWindowText(GetDlgItem(hWnd, IDC_EDIT4), buf);145 }146 147 148 break;149 case WM_COMMAND:150 {151 WORD wmId = LOWORD(wParam);152 WORD wmEvent = HIWORD(wParam);153 switch (wmId)154 {155 case IDC_BUTTON2:156 {157 HWND hCheck = GetDlgItem(hWnd, IDC_CHECK1);158 BOOL bIsCheck = SendMessage(hCheck, BM_GETCHECK, 0, 0);159 if (bIsCheck == 1)160 {161 SendMessage(hCheck, BM_SETCHECK, 0, 0);162 }163 else164 {165 SendMessage(hCheck, BM_SETCHECK, 1, 0);166 }167 break;168 }169 case IDC_BUTTON3:170 {171 HWND hCom = GetDlgItem(hWnd, IDC_COMBO1);172 ComboBox_SelectString(hCom, 0, TEXT("南京"));173 break;174 }175 case IDC_BUTTON4:176 {177 //获取选中的序号178 HWND hCom = GetDlgItem(hWnd, IDC_COMBO1);179 int nId = ComboBox_GetCurSel(hCom);180 181 //获取选中的文本182 TCHAR buf[64] = {};183 TCHAR buf2[64] = {};184 ComboBox_GetText(hCom, buf, 64);185 swprintf_s(buf2, TEXT("选中的序号为:%d,选中的文本为%s"), nId, buf);186 SetWindowText(GetDlgItem(hWnd, IDC_EDIT3), buf2);187 }188 case IDC_BUTTON5:189 {190 191 static int a= 0;192 a += 10;193 if (a == 100)194 {195 a = 0;196 }197 HWND hProce = GetDlgItem(hWnd, IDC_PROGRESS1);198 SendMessage(hProce, PBM_SETPOS, a, 0);199 //SendMessage(hProce, PBM_GETPOS, 0, 0);获取进度条进度200 201 }202 case IDC_BUTTON6:203 {204 HWND hProce = GetDlgItem(hWnd, IDC_SLIDER1);205 //设置滑块刻度范围206 SendMessage(hProce, TBM_SETRANGE, TRUE,207 MAKELONG(0,1000));208 //设置滑块的刻度209 SendMessage(hProce, TBM_SETPOS, TRUE, 20);210 break;211 //得到滑块的位置212 //SendMessage(hProce, TBM_GETPOS, 0, 0);213 }214 case IDC_BUTTON7:215 {216 HWND hProce = GetDlgItem(hWnd, IDC_SLIDER1);217 int nNum = SendMessage(hProce, TBM_GETPOS, 0, 0);218 TCHAR buf[10] = {};219 _stprintf_s(buf, TEXT("%d"), nNum);220 SetWindowText(GetDlgItem(hWnd, IDC_EDIT4), buf);221 }222 }223 }224 break;225 case WM_CLOSE:226 DestroyWindow(hWnd);227 break;228 default:229 break;230 }231 return 0;232 }233 234 235 //236 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)237 //238 // 目的: 处理主窗口的消息。239 //240 // WM_COMMAND - 处理应用程序菜单241 // WM_PAINT - 绘制主窗口242 // WM_DESTROY - 发送退出消息并返回243 //244 //245 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)246 {247 int wmId, wmEvent;248 PAINTSTRUCT ps;249 HDC hdc;250 251 switch (message)252 {253 case WM_CREATE:254 {255 //创建一个按钮256 CreateWindow(TEXT("button"), TEXT("15PB"),257 WS_VISIBLE | WS_CHILD,258 100, 100, 50, 30, hWnd, (HMENU)10001, hInst, NULL);259 //创建一个CheckBox和一个自动的CheckBox260 CreateWindow(TEXT("button"), TEXT("15PB"),261 WS_VISIBLE | WS_CHILD | BS_CHECKBOX,262 160, 100, 50, 30, hWnd, (HMENU)10002, hInst, NULL);263 CreateWindow(TEXT("button"), TEXT("15PB"),264 WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,265 160, 150, 50, 30, hWnd, (HMENU)10004, hInst, NULL);266 //创建一个Radio267 CreateWindow(TEXT("button"), TEXT("15PB"),268 WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON,269 220, 100, 50, 30, hWnd, (HMENU)10003, hInst, NULL);270 //创建一个静态文本271 //也可以使用GetWindowText和SetWindowText获取和设置文本内容272 CreateWindow(TEXT("static"), TEXT("15PB"),273 WS_VISIBLE | WS_CHILD,274 100, 150, 50, 30, hWnd, (HMENU)10005, hInst, NULL);275 //创建一个静态图片276 HWND hStaticPic = CreateWindow(TEXT("static"), TEXT("15PB"),277 WS_VISIBLE | WS_CHILD | SS_BITMAP,278 100, 200, 50, 30, hWnd, (HMENU)10006, hInst, NULL);279 //加载一个bmp格式的位图,可以使用画图板保存为24位的位图280 HBITMAP hBitMap =281 LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP5));282 //将位图资源发送给静态图片控件283 SendMessage(hStaticPic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitMap);284 285 }286 break;287 case WM_COMMAND:288 wmId = LOWORD(wParam);289 wmEvent = HIWORD(wParam);290 // 分析菜单选择: 291 switch (wmId)292 {293 case 10001:294 {295 //创建一个对话框//模态296 DialogBox(hInst,297 MAKEINTRESOURCE(IDD_DIALOG1),298 NULL, (DLGPROC)DlgProc);299 300 }301 break;302 case 10002:303 {304 HWND hCheck = GetDlgItem(hWnd, 10002);305 BOOL bIsCheck = SendMessage(hCheck, BM_GETCHECK, 0, 0);306 if (bIsCheck == 1)307 {308 SendMessage(hCheck, BM_SETCHECK, 0, 0);309 }310 else311 {312 SendMessage(hCheck, BM_SETCHECK, 1, 0);313 }314 }315 case 10003:316 break;317 }318 break;319 case WM_PAINT:320 hdc = BeginPaint(hWnd, &ps);321 // TODO: 在此添加任意绘图代码...322 EndPaint(hWnd, &ps);323 break;324 case WM_DESTROY:325 PostQuitMessage(0);326 break;327 default:328 return DefWindowProc(hWnd, message, wParam, lParam);329 }330 331 return 0;332 }