有没有办法将除CString之外的对象添加到MFC中的CComboBox?

时间:2015-05-29 16:14:24

标签: c++ mfc override controls ccombobox

我正在尝试将具有成员变量CString的对象添加到CCombobox。我不能只是添加字符串,因为我试图与一个工具接口,这个工具要求我有另一个成员变量而不仅仅是一个字符串作为CComboBox中的列表项。以下是我想要做的事情。

CComboBox::AddString(myOwnObject);

我只希望显示myOwnObject的字符串,但是整个对象都在列表框中,以便其他工具可以访问其他成员变量。

2 个答案:

答案 0 :(得分:3)

CComboBox Class包裹了原生Combo Box控件。这是一个相当基本的实现,可以满足最常见的用例:显示字符串供用户选择。

如果您需要其他功能,可以改用CComboBoxEx Class。它公开了底层ComboBoxEx控件的完整操作集。特别是,项目可以配置为根据任意信息在运行时检索项目的字符串表示。

以下假设您的自定义项数据布局如下:

struct CustomItemData {
    CStringW m_Name;
    int m_SomeInteger;
};

项目数据可以是任意复杂的,并保存您想要存储的任何信息。使用项目填充CComboBoxEx需要调用CComboBoxEx::InsertItem,并传递适当填充的COMBOBOXEXITEM structure

// CustomItemData's lifetime must exceed that of the CComboBoxEx; don't use a
// stack-based (automatic) variable.
CustomItemData* pcid = new CustomItemData( myName, myInteger );

CCOMBOBOXEXITEM cbei = { 0 };
cbei.mask = CBEIF_TEXT | CBEIF_LPARAM;
cbei.iItem = currentIndex;  // The zero-based index of the item.
cbei.pszText = LPSTR_TEXTCALLBACK;  // The control will request the information by using
                                    // the CBEN_GETDISPINFO notification codes.
cbei.lParam = reinterpret_cast<LPARAM>( pcid );  // Assign custom data to item.
myComboBox.InsertItem( &cbei );

此时,ComboBox控件将填充项目,并将从应用程序请求显示信息。 CBEN_GETDISPINFO被发送到控件 parent ,因此通知处理程序必须放在父窗口(通常是对话框)实现中。处理程序使用ON_NOTIFY宏连接到通知消息:

// Inside the parent's message map:
ON_NOTIFY( CBEN_GETDISPINFO, IDC_MY_COMBOBOX, GetCBDispString )

// Message handler inside the parent's class
void CMyDlg::GetCBDispString( NMHDR* pNMHDR, LRESULT* pResult ) {
    NMCOMBOBOXEX* pncbe = reinterpret_cast<NMCOMBOBOXEX*>( pNMHDR );
    COMBOBOXEXITEM& cbei = pncbe->ceItem;
    if ( cbei.mask & CBEIF_TEXT ) {
        // Text is requested -> fill the appropriate buffer.
        const CustomItemData& cd = *reinterpret_cast<const CustomItemData*>( cbei.lParam );
        wcscpy( cbei.pszText, cd.m_Name );
        // Prevent future callbacks for this item. This is an optional optimization
        // and can be used, if the m_Name member doesn't change.
        cbei |= CBEIF_DI_SETITEM;
    }
    // Mark notification as handled
    *pResult = 0;
}


有时需要将CBEN_GETDISPINFO回调放在自定义ComboBox实现中。 MFC提供了实现消息反射所必需的基础结构(参见TN062: Message Reflection for Windows Controls)。这允许父窗口将通知消息反映回相应的子控件以进行处理。它有时很有用,但不需要实现这个问题的解决方案。


如果您不需要在运行时完全控制构造显示字符串,则可以使用简单的CComboBox控件,并附加调用CComboBox::SetItemData或{的其他信息{3}},如CComboBox::SetItemDataPtr

中所示

答案 1 :(得分:0)

  

&#34;我认为需要做的是覆盖CComboBox::AddString&#34;

不,反过来说,你不能覆盖 CComboBox::AddString, 但是实现其他对象类型以获得适当的conversion operator

operator const CString& () const { return myCStringMember; }

您可能还想使用CComboBox::SetItemData()功能来存储关联的类实例&#39; this指针。

你可以使用`CComboBox :: AddString()`的返回值来获取调用所需的索引

int SetItemData( 
    int nIndex, // <<< Fill in result of AddString
    DWORD_PTR dwItemData  
 );

应该看起来像这样(假设你有如上所述实现的转换运算符):

MyOwnObject myOwnObject("MyOwnObject1");

int newItemIndex = comboBox.AddString(myOwnObject);
if(newItemIndex > 0) {
    comboBox.SetItemData(newItemIndex,(DWORD_PTR)&myOwnObject)
}
相关问题