SendMessage函数向窗口发送消息
LRESULT SendMessage(
HWND hWnd, // handle to destination window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
1 向Combo Box添加数据
HWND hWndComboBox = GetDlgItem(hWnd, IDC_COMBO1);
TCHAR szMessage[20] = "Hello";
SendMessage(hWndComboBox , CB_ADDRSTRING, 0, (LPARAM)szMessage);
2 向Combo Box插入数据
HWND hWndComboBox = GetDlgItem(hWnd, IDC_COMBO1);
TCHAR szMessage[20] = "World";
SendMessage(hWndComboBox , CB_INSERTRSTRING, 0, (LPARAM)szMessage);
3向Combo Box删除数据
SendMessage(hWndComboBox, CB_DELETESTRING, 1, 0); //删除第二项数据
4 清除Combo Box所有数据
SendMessage(hWndComboBox, CB_RESETCONTENT, 0, 0);
5 获取Combo Box数据项目的数量
UINT uCount;
uCount = SendMessage(hWndComboBox, CB_GETCOUNT, 0, 0):
6 获取Combo Box某项的值
TCHAR szMessage[200];
ZeroMessage(szMessage, sizeof(szMessage)):
SendMessage(hWndComboBox, CB_GETLBTEXT, 1, (LPARAM)szMessage); //获取第二项的数据
MessageBox(NULL, szMessage, " ", MB_OK);
7 //设置组合框和列表框中默认选中的项
SendMessage(hwndComboBox, CB_SETCURSEL, 0, 0); SendMessage(hwndList, LB_SETCURSEL, 0, 0); SetFocus(hwndList);
8 使Combo box控件可见或不可见,需使用EnablkeWindow函数: EnableWindow(hCombo,TRUE); EnableWindow(hCombo,FALSE);
9 // 改变静态文本控件的显示内容 SendMessage(hwndStatic, WM_SETTEXT, 0,(LPARAM)introduce[0]);
10 响应Combo box的Notification message,比如选择Combo box中一个不同于当前的Item时,会响应CBN_SELCHANGE消息。 MSDN的解释:
CBN_SELCHANGE Notification
The CBN_SELCHANGE notification message is sent when the user changes the current selection in the list box of a combo box. The user can change the selection by clicking in the list box or by using the arrow keys. The parent window of the combo box receives
this notification in the form of aWM_COMMAND message with CBN_SELCHANGE in the high-order word of the wParam parameter. Syntax
CBN_SELCHANGE
WPARAM wParam LPARAM lParam; Parameters
wParam
The low-order word specifies the control identifier of the combo box.
The high-order word specifies the notification message.
lParam
Handle to the combo box.
Process Message Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
// low-order word specifies the control identifier of the combo box.
wmId = LOWORD(wParam);
//high-order word specifies the notification message.
wmEvent = HIWORD(wParam); // 分析菜单选择:
switch (wmEvent) {
case CBN_SELCHANGE:
if (wmId==IDC_COMBO_MODE) //判断选中的是哪个Combo box {
. . . . . . }
break; }
break;
case WM_DESTROY: PostQuitMessage(0); break;
//Although the dialog box procedure is similar to a window procedure,
//it must not call the DefWindowProc function to process unwanted messages // default:
// return DefWindowProc(hWnd, message, wParam, lParam); }
return 0; }
11 // 获得组合框中选中的项的索引,并重新设置列表框中的项 cbIndex = SendMessage(hwndComboBox, CB_GETCURSEL, 0, 0); SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
12 // 对于后来用户添加的项
SendMessage(hwndList, LB_RESETCONTENT, 0, 0); SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)TEXT("目前尚未完成"));// 重新设置静态文本框
SendMessage(hwndStatic, WM_SETTEXT, 0, (LPARAM)TEXT(" ")); }
// 改变静态文本控件的显示内容
SendMessage(hwndList, LB_SETCURSEL, 0, 0); SendMessage(hwndStatic, WM_SETTEXT, 0, (LPARAM)introduce[cbIndex]);
13
// 获取组合框中编辑框部分内的文本SendMessage(hwndComboBox, WM_GETTEXT, 20 , (LPARAM)temp);
// 将新项添入组合框
SendMessage(hwndComboBox, CB_ADDSTRING, 0, (LPARAM)temp); SendMessage(hwndComboBox, CB_SETCURSEL, 0, 0); bChanged = FALSE } break; }
return 0
组合框类型为下拉式组合框,其创建风格为下列值的组合:
WS_CHILD | WS_VISIBLE |WS_VSCROLL | WS_TABSTOP | CBS_DROPDOWN 在WM_SIZE消息处理期间,窗口过程设置组合框和列表框的位置和大小,并通过向控件发送消息来初始化组合框和列表框中的选项,如下所示:
SendMessage(hwndComboBox, CB_RESETCONTENT, 0, 0 );
SendMessage(hwndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("四川")); …
SendMessage(hwndComboBox, CB_SETCURSEL, 0, 0); …
注意:在重新设置组合框或者列表框中的选项时,向它们发送CB_ RESETCONTENT消息非常重要,否则,将会把选项列表重复地添加到组合框或者列表框中。CB_SETCURSEL消息将索引为零的选项设置为默认的选