文章来源:http://bbs.csdn.net/topics/310154805
问题提出:
我继承了CComboBox类,想在OnCreate里改变ComboBox为DropDown List。
但OnCreate根本不被调用?为啥?我的代码如下:
BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
ON_WM_CREATE()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int CMyComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CComboBox::OnCreate(lpCreateStruct) == -1)
return -1;
this->ModifyStyle(CBS_DROPDOWN,CBS_DROPDOWNLIST);
return 0;
}
我在Dialog的代码中用ModifyStyle也不行,请给完整代码!
m_cmbUser.ModifyStyle(CBS_DROPDOWNLIST);
解决方案:
方法1、
重载PreSubClassWindow()
在这里面写 ModifyStyle(CBS_DROPDOWN,CBS_DROPDOWNLIST);
方法2、
动态修改ComboBox的DROPDOWN,DROPDOWNLIST属性
DWORD theStyle;
HWND theChild;
theChild = ::GetWindow( GetDlgItem(IDC_COMBO1)->m_hWnd , GW_CHILD );
theStyle = GetWindowLong(GetDlgItem(IDC_COMBO1)->m_hWnd, GWL_STYLE );
theStyle &= ~CBS_DROPDOWN;//去掉DROPDOWN
theStyle |= CBS_DROPDOWNLIST; //添加DROPDOWNLIST
::DestroyWindow( theChild );
SetWindowLong(GetDlgItem(IDC_COMBO1)->m_hWnd, GWL_STYLE , theStyle );