自己自定义了一个CComboBox的子控件,不知道哪里出问题了,求高手帮忙看一下。
我从CComboBox派生了一个子控件,目的是在输入的时候能够列举相似的选项,并以下拉菜单的方式显示出来,程序响应CBN_EDITCHANGE消息,在输入的时候查找一个set中的数据,将所有相似的字符串添加到CComboBox中并弹出菜单,但是不知道为啥我在类的访问到的set都是空。请大神们帮忙看看。我已经想了半天了。
类申明
typedef set< string > str_set;
/////////////////////////////////////////////////////////////////////////////
// CPopupEdit window
class CPopupEdit : public CComboBox
{
// Construction
public:
CPopupEdit();
// Attributes
public:
afx_msg void OnEditChange();
virtual ~CPopupEdit();
public:
BOOL PreTranslateMessage(MSG* pMsg);
void SetPopupEditObserver(CPopupEditObserver* aObserver);
void AddPopupOption(string str);
protected:
void UpdatePopupMenu();
protected:
DECLARE_MESSAGE_MAP()
protected:
str_set m_strsDataSource;
CPopupEditObserver* m_peoObserver;
};
CPopupEdit::CPopupEdit()
:m_peoObserver(NULL)
{
AddPopupOption(string("hello"));
AddPopupOption(string("world"));
AddPopupOption(string("FaultCodeGeneratorCC"));
}
CPopupEdit::~CPopupEdit()
{
}
BEGIN_MESSAGE_MAP(CPopupEdit, CComboBox)
//{{AFX_MSG_MAP(CPopupEdit)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPopupEdit message handlers
class string_finder
{
public:
string_finder(string& str):m_str(str){}
bool operator()(string str)
{
return m_str.find(str)==0;
}
protected:
private:
string m_str;
};
void CPopupEdit::SetPopupEditObserver(CPopupEditObserver* aObserver)
{
m_peoObserver=aObserver;
}
void CPopupEdit::AddPopupOption(string str)
{
m_strsDataSource.insert(str);
}
void CPopupEdit::UpdatePopupMenu()
{
if (0==m_strsDataSource.size()) {
return;
}
CString str;
GetWindowText(str);
if (str=="") {
return;
}
str_set::iterator itor=find_if(m_strsDataSource.begin(),m_strsDataSource.end(),string_finder(string(str.GetBuffer(str.GetLength()))));
if (itor==m_strsDataSource.end())
{
this->ShowDropDown(FALSE);
return;//not found
}
this->ResetContent();
for (int ii=0; itor!=m_strsDataSource.end(); ++itor, ++ii) {
this->InsertString(ii,itor->c_str());
}
this->ShowDropDown(TRUE);
}
BOOL CPopupEdit::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg->message)
{
case WM_KEYDOWN:
{
switch(pMsg->wParam)
{
case VK_RETURN:
if(m_peoObserver)
m_peoObserver->HandleInputCompleted();
break;
default:
break;
}
}
break;
default:
break;
}
return CComboBox::PreTranslateMessage(pMsg);
}
void CPopupEdit::OnEditChange()
{
UpdatePopupMenu();
}