ComBoBox消息触发事件

更新时间:2023-11-22 11:58:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

void CEXERCISE1Dlg::OnSelchangeCombo1() {

int bnt,receivenum; CString anyview;

bnt = m_list.GetCurSel(); if (bnt != CB_ERR) {

m_list.GetLBText(bnt,m_test);

receivenum = m_list.GetItemData(bnt); anyview.Format(\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的值等,请参考: http://blog.csdn.net/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,

本文来源:https://www.bwwdw.com/article/8hpv.html

Top