16 Windows编程——系统内置窗口子类型之editComboBox
Posted kelamoyujuzhen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了16 Windows编程——系统内置窗口子类型之editComboBox相关的知识,希望对你有一定的参考价值。
edit类型的子窗口
ES_MULTILINE:多行输入文本框
窗口的消息:
WL_COMMAND:
EN_CHANGE:当edit窗口内的文本内容改变的时候,edit子窗口给父窗口发送一个WL_COMMAND消息,其通知码是EN_CHANGE。
WM_GETTEXT:
用SendMessage函数给edit窗口发送这个消息,同时,给wparam参数传递我们想要获得的字符个数,lParam传递一个内存块地址,sendMessage函数返回后,
lParam指向的内存中,将保存获得的字符。
WL_GETTEXTLENGTH:
用 SendMessage函数给edit窗口发送这个消息,将wParam和1Param都赋值为NULL,那SendMessage函数,将返回edit中的字符个数,但这个字符个数不包括NULL结尾字符。
edit空间演示源码
1 #include<Windows.h> 2 3 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 4 5 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 6 { 7 WNDCLASS WndClass; 8 TCHAR* ClassName = TEXT("MyClass"); 9 HWND hwnd; 10 MSG msg; 11 HBRUSH hBrush; 12 hBrush = CreateSolidBrush(RGB(249,98,241)); 13 14 WndClass.cbClsExtra = 0; 15 WndClass.cbWndExtra = 0; 16 WndClass.hbrBackground = hBrush; 17 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 18 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 19 WndClass.hInstance = hInst; 20 WndClass.lpfnWndProc = WindProc; 21 WndClass.lpszClassName = ClassName; 22 WndClass.lpszMenuName = NULL; 23 WndClass.style = CS_VREDRAW | CS_HREDRAW; 24 25 if (!RegisterClass(&WndClass)) 26 { 27 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 28 return 0; 29 } 30 31 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 32 if (hwnd == NULL) 33 { 34 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 35 return 0; 36 } 37 ShowWindow(hwnd, nShow); 38 UpdateWindow(hwnd); 39 40 while (GetMessage(&msg, NULL, 0, 0)) 41 { 42 TranslateMessage(&msg); 43 DispatchMessage(&msg); 44 } 45 46 return 0; 47 } 48 49 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 50 { 51 HDC hdc; 52 static int cx, cy; 53 static editHwnd, staticHwnd; 54 TCHAR str[1024]; 55 int length; 56 switch (message) 57 { 58 case WM_CREATE: 59 editHwnd = CreateWindow(TEXT("edit"),NULL, WS_CHILD | WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL , 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL); 60 staticHwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE|WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL); 61 return 0; 62 case WM_SIZE: 63 cx = LOWORD(lParam); 64 cy = HIWORD(lParam); 65 MoveWindow(editHwnd, 0, 0, cx/2, cy/2, TRUE); 66 MoveWindow(staticHwnd, cx / 2, cy / 2, cx, cy, TRUE); 67 return 0; 68 case WM_COMMAND: 69 if (HIWORD(wParam)==EN_CHANGE) 70 { 71 length = SendMessage(editHwnd, WM_GETTEXTLENGTH, NULL, NULL) + 1; 72 length = length > 1023 ? 1023 : length; 73 SendMessage(editHwnd, WM_GETTEXT, length, (LPARAM)str) ; 74 str[length] = ‘/0‘; 75 SetWindowText(staticHwnd,str); 76 } 77 return 0; 78 case WM_DESTROY: 79 PostQuitMessage(0); 80 return 0; 81 default: 82 break; 83 } 84 85 return DefWindowProc(hwnd, message, wParam, lParam); 86 }
运行结果
ComboBox类型的子窗口
ComboBox子窗口的三种主要子类型:
CB_ADDSTRING:在combobox控件的list框中,增加一个字符串,wParam=NULL,lParam符串的指针。
WL_COMMAND:
CBN_SELCHANGE:当combobox中的选项发生变化时,发送这个通知码,其中LOWORD(lParam)就是这个combobox的窗口标识ID。
CB_GETCURSEL:用SendMessage函数,发送这个消息给combobox子窗口,那么这个函数将返回被选中的列表项的索引(索引是从0开始的)。wParam=lParam=NULL.
演示代码
1 #include<Windows.h> 2 #include<WinUser.h> 3 #include<tchar.h> 4 #include<stdio.h> 5 6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 7 8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 9 { 10 WNDCLASS WndClass; 11 TCHAR* ClassName = TEXT("MyClass"); 12 HWND hwnd; 13 MSG msg; 14 15 WndClass.cbClsExtra = 0; 16 WndClass.cbWndExtra = 0; 17 WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 18 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 19 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 20 WndClass.hInstance = hInst; 21 WndClass.lpfnWndProc = WindProc; 22 WndClass.lpszClassName = ClassName; 23 WndClass.lpszMenuName = NULL; 24 WndClass.style = CS_VREDRAW | CS_HREDRAW; 25 26 if (!RegisterClass(&WndClass)) 27 { 28 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 29 return 0; 30 } 31 32 //CreateWindow返回之前,会发送WM_CREATE消息 33 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 34 if (hwnd == NULL) 35 { 36 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 37 return 0; 38 } 39 ShowWindow(hwnd, nShow); 40 UpdateWindow(hwnd); 41 42 while (GetMessage(&msg, NULL, 0, 0)) 43 { 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 48 return 0; 49 } 50 51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 52 { 53 PAINTSTRUCT pt; 54 HDC hdc; 55 static HWND ComboHwnd1, ComboHwnd2, ComboHwnd3,button,staticHwnd; 56 TCHAR str[1024]; 57 switch (message) 58 { 59 case WM_CREATE: 60 ComboHwnd1 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_SIMPLE|CBS_AUTOHSCROLL|WS_VSCROLL, 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL); 61 ComboHwnd2 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN, 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL); 62 ComboHwnd3 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 0, 0, 0, 0, hwnd, (HMENU)3, GetModuleHandle(NULL), NULL); 63 button = CreateWindow(TEXT("button"), TEXT("OK"), WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hwnd, (HMENU)4, GetModuleHandle(NULL), NULL); 64 staticHwnd= CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)5, GetModuleHandle(NULL), NULL); 65 return 0; 66 case WM_SIZE: 67 MoveWindow(ComboHwnd1, 0, 0, 100, 100, TRUE); 68 MoveWindow(ComboHwnd2, 150, 0, 100, 150, TRUE); 69 MoveWindow(ComboHwnd3, 300, 0, 100, 150, TRUE); 70 MoveWindow(button, 20, 120, 50, 25, TRUE); 71 MoveWindow(staticHwnd, 0, 150, 100, 100, TRUE); 72 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-1")); 73 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-2")); 74 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-3")); 75 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-4")); 76 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-5")); 77 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-6")); 78 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-7")); 79 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-8")); 80 SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-9")); 81 return 0; 82 case WM_COMMAND: 83 if (HIWORD(wParam) == CBN_SELCHANGE) 84 { 85 GetWindowText(ComboHwnd1,str,50); 86 SetWindowText(staticHwnd,str); 87 } 88 return 0; 89 case WM_DESTROY: 90 PostQuitMessage(0); 91 return 0; 92 default: 93 break; 94 } 95 96 return DefWindowProc(hwnd, message, wParam, lParam); 97 }
运行结果:
以上是关于16 Windows编程——系统内置窗口子类型之editComboBox的主要内容,如果未能解决你的问题,请参考以下文章
深入delphi编程理解之消息WINDOWS原生窗口编写及消息处理过程
《逐梦旅程 WINDOWS游戏编程之从零开始》读书笔记1——创建窗口
Windows API编程(不是MFC)用CreateWindowEx创建的按钮等子窗口控件是默认的Windows经典样式,如何改变?