1. 在你的工程中添加ComboListCtrl.cpp ComboListCtrl.h InPlaceCombo.cpp InPlaceCombo.h InPlaceEdit.cpp InPlaceEdit.h六个文件。(工程-增加到工程-文件...)
2. *dlg.h(把*改为你的工程名)中添加 #include "ComboListCtrl.h"
3. 为你的List控件添加CComboListCtrl类型的变量。(先添加为ListCtrl类型,再到*dlg.h中找到ListCtrl m_ListCtrl,手动改为CComboListCtrl m_ListCtrl)
4. 添加自定义消息。
*dlg.h中:
class CTestComboListCtrlDlg : public CDialog
{
...
//}}AFX_MSG
afx_msg LRESULT OnEndLabelEditVariableCriteria(WPARAM wParam, LPARAM lParam); //添加的自定义的消息
afx_msg LRESULT PopulateComboList(WPARAM wParam, LPARAM lParam); //添加的自定义的消息
DECLARE_MESSAGE_MAP()
};
*dlg.cpp中:
BEGIN_MESSAGE_MAP(CTestComboListCtrlDlg, CDialog)
...
//}}AFX_MSG_MAP
ON_MESSAGE(WM_VALIDATE, OnEndLabelEditVariableCriteria) //添加的自定义的消息
ON_MESSAGE(WM_SET_ITEMS, PopulateComboList) //添加的自定义的消息
END_MESSAGE_MAP()
LRESULT *Dlg::OnEndLabelEditVariableCriteria(WPARAM wParam, LPARAM lParam)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
// TODO: Add your control notification handler code here
return 1;
}
LRESULT *Dlg::PopulateComboList(WPARAM wParam, LPARAM lParam)
{
// Get the Combobox window pointer
CComboBox* pInPlaceCombo = static_cast<CComboBox*> (GetFocus());
// Get the inplace combbox top left
CRect obWindowRect;
pInPlaceCombo->GetWindowRect(&obWindowRect);
CPoint obInPlaceComboTopLeft(obWindowRect.TopLeft());
// Get the active list
// Get the control window rect
// If the inplace combobox top left is in the rect then
// The control is the active control
m_ListCtrl.GetWindowRect(&obWindowRect); //m_ListCtrl改为你的变量名
int iColIndex = (int )wParam;
CStringList* pComboList = reinterpret_cast<CStringList*>(lParam);
pComboList->RemoveAll();
if (obWindowRect.PtInRect(obInPlaceComboTopLeft))
{
if(iColIndex==3)
{
pComboList->AddTail("男"); //修改为你的选项
pComboList->AddTail("女");
}
}
return true;
}
5. 添加List控件的初始化设置:
例如:
BOOL *Dlg::OnInitDialog()
{
...
// TODO: Add extra initialization here
//m_ListCtrl
m_ListCtrl.InsertColumn(0, "编号", LVCFMT_LEFT, 60);
m_ListCtrl.InsertColumn(1, "姓名", LVCFMT_LEFT, 150);
m_ListCtrl.InsertColumn(2, "学号", LVCFMT_LEFT, 150);
m_ListCtrl.InsertColumn(3, "性别", LVCFMT_LEFT, 150);
CString strValidChars;//
m_ListCtrl.SetReadOnlyColumns(0);//read only
strValidChars = "";
m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,1);//none control edit
strValidChars = "0123456789.";
m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,2);//digital only edit
m_ListCtrl.SetComboColumns(3,TRUE);
m_ListCtrl.EnableVScroll();
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
CString str;
for(int i=0;i<30;i++)
{
str.Format("%d",i+1);
m_ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, i,
str, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED, 0, 0);
m_ListCtrl.SetItemText(i,1,"张三");
m_ListCtrl.SetItemText(i,2,"1234");
m_ListCtrl.SetItemText(i,3,"男");
}
return TRUE; // return TRUE unless you set the focus to a control
}
6. 好了,可以运行看看