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);
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("目前尚未完成"));
13
// 获取组合框中编辑框部分内的文本原文地址:http://blog.csdn.net/keebai/article/details/41521683