2024年2月26日发(作者:)

它是来响应你在组合框里选择的事件函数。

void CEXERCISE1Dlg::OnSelchangeCombo1()

{

int bnt,receivenum;

CString anyview;

bnt = m_Sel();

if (bnt != CB_ERR)

{

m_ext(bnt,m_test);

receivenum = m_mData(bnt);

("%d",receivenum);

m_test2 = anyview;

UpdateData(FALSE);

}

else

{

MessageBox("您未选中任何一项!");

}

}

3、注意的是:m_list为组合框的一个成员变量,类型为:CComoboBox. m_test和m_test2为两个编辑框的成员变量,类型为:CString。

消息触发事件

ON_CBN_SELENDOK

當應用程式接收 CBN_SELCHANGE 通知訊息時,尚未更新下拉式方塊的編輯/靜態部份。若要取得新的選取範圍,傳送 CB_GETLBTEXT 訊息至下拉式方塊控制項。這封郵件會放在新的選取範圍的文字中指定的緩衝區。下列是簡短的程式碼片段:

... /* Other code. */

case CBN_SELCHANGE:

hCombo = LOWORD(lParam); /* Get combo box window handle. */

/* Get index of current selection and the text of that selection.

*/

index = SendMessage(hCombo, CB_GETCURSEL, (WORD)0, 0L);

SendMessage(hCombo, CB_GETLBTEXT, (WORD)index,

(LONG)buffer);

break;

... /* Other code. */

Win32程序中使用Combo box控件

分类: VC/MFC 2012-02-17 11:36 91人阅读 评论(0) 收藏 举报

第一次使用win32写代码,将代码中对Combo box 控件的使用做个总结:

1. 使用SendMessage向窗口发送消息,对Combo Box进行基本操作如添加数据,删除数据,得到所选Item的值等,请参考:

/qiurisuixiang/article/details/6746234

2. 使Combo box控件可见或不可见,需使用EnablkeWindow函数:

EnableWindow(hCombo,TRUE);

EnableWindow(hCombo,FALSE);

3. 响应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;

}

// 在组合框中添加项

SendMessage(hwndComboBox, CB_RESETCONTENT, 0, 0 );

SendMessage(hwndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("四川"));

SendMessage(hwndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("广东"));

SendMessage(hwndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("河南"));

//设置组合框和列表框中默认选中的项

SendMessage(hwndComboBox, CB_SETCURSEL, 0, 0);

SendMessage(hwndList, LB_SETCURSEL, 0, 0);

SetFocus(hwndList);

// 改变静态文本控件的显示内容

SendMessage(hwndStatic,

(LPARAM)introduce[0]);

return 0 ;

case WM_COMMAND: // 处理控件通知消息

switch(LOWORD(wParam))

{

case IDC_MYCOMBOX: // 组合框控件的通知消息

switch(HIWORD(wParam))

{

WM_SETTEXT, 0,

case CBN_SELCHANGE: // 选中的项已经改变

// 获得组合框中选中的项的索引,并重新设置列表框中的项

cbIndex = SendMessage(hwndComboBox, CB_GETCURSEL, 0, 0);

SendMessage(hwndList, LB_RESETCONTENT, 0, 0);

if(cbIndex < 3) // 对于组合框内原有的前三项

{

for(i = 0 ; i < NUM[cbIndex] ; i++)

{

SendMessage(hwndList, LB_ADDSTRING, 0,

(LPARAM)place[cbIndex][i]);

}

}

else // 对于后来用户添加的项

{

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]);

break;

case CBN_EDITCHANGE : // 用户改变了组合框中的编辑控件的内容

bChanged = TRUE ; // 设置改动标识

break;

}

break ;

case IDC_MYLIST: // 列表框控件通知消息

switch(HIWORD(wParam))

{

case LBN_DBLCLK: // 双击列表框中的项

// 获取双击的列表项的文本

lbIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);

SendMessage(hwndList, LB_GETTEXT, lbIndex, (LPARAM)temp);

wsprintf(buffer, "%s 是一个好地方", temp);

MessageBox(hwnd, buffer, "欢迎光临", MB_OK);

break;

}

break;

case IDC_MYBUTTON: // 按钮控件通知消息

SendMessage(hwnd, WM_CLOSE, wParam, lParam);

break;

case IDC_ADDBUTTON: // 向组合框中添加新项

if(bChanged)

{

// 获取组合框中编辑框部分内的文本

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 ;

从上可以看到,程序StdControl3没有使用任何资源,相关标识符的值和一些字符数组在头文件StdControl3.h中定义。组合框类型为下拉式组合框,其创建风格为下列值的组合:

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消息将索引为零的选项设置为默认的选