MFC Extend CcomboBox

来源:互联网 发布:理性 知乎 编辑:程序博客网 时间:2024/06/07 17:57

ComboBoxEx虽然继承与CComboBox,但不可以使用CComboBox的如下函数:(使用时产生ASSERT断言失败)

_AFXCMN_INLINE int CComboBoxEx::Dir(UINT attr, LPCTSTR lpszWildCard)


_AFXCMN_INLINE int CComboBoxEx::FindString(int nIndexStart, LPCTSTR lpszFind) const

_AFXCMN_INLINE int CComboBoxEx::AddString(LPCTSTR lpszString)

_AFXCMN_INLINE BOOL CComboBoxEx::SetEditSel(int nStartChar, int nEndChar)

_AFXCMN_INLINE int CComboBoxEx::InsertString(int nIndex, LPCTSTR lpszString)


  Extended Combo Box的功能比Combo Box更多一点.其中有个功能就是可以在选项前加个图标.

                先添加一个Extended Combo Box控件.并给它添加一个变量cbBoxE.

                CImageList* pImageList;      //相当于一个特殊数组,但是它保存的是图片

                pImageList = new CImageList();

                pImageList->Create(32, 32, ILC_COLOR32, 0, 4); //

                //在资源中添加一个bmp图片.id为IDB_BITMAP1

                CBitmap bmp;

                bmp.LoadBitmap(IDB_BITMAP1); //加载资源中的图片

                pImageList->Add(&bmp, RGB(0, 0, 0)); //添加图片到imagelist中

                cbBoxE.SetImageList(pImageList);

                COMBOBOXEXITEM insItem; //添加图片选项时的参数.它是一个结构体,下面是里面一些变量的初始化

                insItem.mask = CBEIF_IMAGE|CBEIF_TEXT;

                insItem.iItem=0;

                insItem.iImage=0;

                insItem.pszText=_T("Line 1");

                cbBoxE.InsertItem(&insItem);//在控件中添加一个选项,并且选项前面有图片


消息响应要点:<pre style="white-space: pre-wrap; word-wrap: break-word;">

1.<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; line-height: 24px; text-indent: 28px;">ON_CBN_SELCHANGE用户在列表中单击或者利用箭头键选择了另一个项,从而引起了选中项的改变。</span><span style="color: rgb(255, 0, 0);"><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; text-indent: 28px;">在处理此消息时,编辑控件中的</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/300107.htm" style="color: rgb(106, 57, 6); text-decoration: none; font-family: arial, 宋体, sans-serif; line-height: 24px; text-indent: 28px;">文本</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; text-indent: 28px;">只能通过GetLBText或者类似的某个函数存取。不能使用GetWindowText函数。实践表明:这时调用GetWindowText函数可以拿到正确的文本。</span></span>
<span style="font-family: arial, 宋体, sans-serif; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51);">2.</span><span style="color: rgb(51, 51, 51);">ON_CBN_EDITCHANGE用户发出了一个可能改变编辑控件中文本的动作。与消息CBN_EDITUPDATE不同,本消息是在Windows刷新</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/207233.htm" style="color: rgb(19, 110, 194); text-decoration: none;">屏幕</a><span style="color: rgb(51, 51, 51);">之后发出的。风格为CBS_DROPDOWNLIST的</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/1436669.htm" style="color: rgb(19, 110, 194); text-decoration: none;">组合框</a><span style="color: rgb(51, 51, 51);">没有该通知。</span><span style="color: rgb(255, 0, 0);">实践表明,ON_CBN_EDITCHANGE在ON_CBN_SELECTCHANGE之前进行响应。</span></span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; color: rgb(255, 0, 0); text-indent: 28px;">实践表明:这时调用GetWindowText函数可以拿到正确的文本。</span>
<span style="font-family: arial, 宋体, sans-serif; line-height: 24px;"><span style="text-indent: 28px;"><span style="color: rgb(51, 51, 51);">3.</span><span style="color: rgb(51, 51, 51);">ON_CBN_SELENDOK用户在选择了一项以后,要么按了ENTER键,要么按了向下箭头键,从而隐藏了</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/1436669.htm" style="color: rgb(19, 110, 194); text-decoration: none;">组合框</a><span style="color: rgb(51, 51, 51);">的列表。此通知消息在CBN_CLOSEUP之前发送,表明用户的选择将不被认为是有效的。即使没有发送CBN_CLOSEUP通知消息,也会发送CBN_SELENDCANCEL或CBN_SEL-ENDOK消息(例如风格为CBS_SIMPLE的</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/1436669.htm" style="color: rgb(19, 110, 194); text-decoration: none;">组合框</a><span style="color: rgb(51, 51, 51);">)。</span><span style="color: rgb(255, 0, 0);">实践表明:ON_CBN_SELENDOK在ON_CBN_EDITCHANGE之前进行响应。实践表明:这时调用GetCurSel拿到正确的索引,再用GetLBText拿到选择的文本,而用GetWindowText拿到之前的文本,不是选择后显示的文本,因为这时控件中的Edit部分还没有更新显示。</span></span></span>
<span style="font-family: arial, 宋体, sans-serif; line-height: 24px;"><span style="text-indent: 28px;">3.在调用CComboBox::SetCurSel()更新选择时不会立即发送CBN_SELCHANGE消息,因此调用SetCurSel()来触发CBN_SELCHANGE消息响应是错误的。调用SetCurSel()时只是更新了CComboBox内部的索引值,该索引值就是通过GetCurSel()获得的值。即使调用UpdateData也不行。</span></span>
<span style="font-family: arial, 宋体, sans-serif; line-height: 24px;"><span style="text-indent: 28px;">4.对于DropDownList风格的组合框不会触发CBN_EDITCHANGE消息发送(即使是选择触发编辑框内容被改变也不会发送),对于其他风格的组合框才会在内容改变时触发CBN_EDITCHANGE消息(用ON_CBN_EDITCHANGE响应)。</span></span>

0 0
原创粉丝点击