1、新建对话框项目 listcombox
1.1 在对话框上添加编辑控件 IDC_EDIT1、添加组合框控件、添加列表控件(View=Report)
1.2 类向导建立成员变量,m_Edit,m_Cmb,m_listctrl
2、对话框初始化时,添加如下代码
// TODO: 在此添加额外的初始化代码
m_listctrl.InsertColumn(0,L"item1",LVCFMT_CENTER,100);
m_listctrl.InsertColumn(1,L"item2",LVCFMT_CENTER,100);
m_listctrl.InsertColumn(2,L"item3",LVCFMT_CENTER,100);
m_listctrl.InsertColumn(3,L"item4",LVCFMT_CENTER,100);
m_listctrl.InsertItem(0,L"sub11");
m_listctrl.SetItemText(0,1,L"sub12");
m_listctrl.InsertItem(1,L"sub21");
m_listctrl.SetItemText(1,3,L"sub24");
m_Cmb.SetParent(&m_listctrl); //确保 CComboBox 的坐标是相对于列表控件而言的。
m_Edit.SetParent(&m_listctrl);//确保 Edit 控件的坐标是相对于列表控件而言的。
3、类向导添加事件
3.1 列表框控件添加单击事件 NM_CLICK
=
void ClistcomboxDlg::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
pNMItemActivate
LPNMITEMACTIVATE
reinterpret_cast(pNMHDR);
// TODO: Add your control notification handler code here
POINT PT;
GetCursorPos(&PT);
m_listctrl.ScreenToClient(&PT);
LVHITTESTINFO hitInfo;
hitInfo.pt=PT;
//hitem=m_Tree.GetSelectedItem();
m_listctrl.SubItemHitTest(&hitInfo);
if(hitInfo.flags & LVHT_ONITEMLABEL)//判断是否单击在文本上
{
CRect rect;
m_listctrl.GetSubItemRect(hitInfo.iItem,hitInfo.iSubItem,LVIR_BOUNDS,rect);
if (hitInfo.iSubItem==0)
{
rect.right=rect.left+m_listctrl.GetColumnWidth(0);//每列宽度不相同的,请写在每列中
}
else if(hitInfo.iSubItem==1)//对列进行判断在这里我是 1,2 为 edit,3,4 为 combo box
{
CString mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);
// rect.InflateRect(0,0,0,0);//增大组合框的高度使其可以容纳整行文本。
m_col=hitInfo.iSubItem;
m_row=hitInfo.iItem;
m_Edit.MoveWindow(&rect,TRUE);
m_Edit.ShowWindow(SW_NORMAL);
m_Edit.SetWindowText(mes);
m_Edit.BringWindowToTop();
m_Edit.SetFocus();//使组合框聚焦
}
else if(hitInfo.iSubItem==2)
{
CString mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);
// rect.InflateRect(0,0,0,0);//增大组合框的高度使其可以容纳整行文本。
m_col=hitInfo.iSubItem;
m_row=hitInfo.iItem;
m_Edit.MoveWindow(&rect,TRUE);
m_Edit.ShowWindow(SW_NORMAL);
m_Edit.SetWindowText(mes);
m_Edit.BringWindowToTop();
m_Edit.SetFocus();//使组合框聚焦
}
else if(hitInfo.iSubItem==3)
{
CString mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);
// rect.InflateRect(0,0,0,0);//增大组合框的高度使其可以容纳整行文本。
m_col=hitInfo.iSubItem;
m_row=hitInfo.iItem;
m_Cmb.MoveWindow(&rect,TRUE);
m_Cmb.ShowWindow(SW_NORMAL);
m_Cmb.SetWindowText(mes);
m_Cmb.BringWindowToTop();
m_Cmb.SetFocus();//使组合框聚焦
}
}
*pResult = 0;
}
3.2 组合框添加失去焦点事件 CBN_KILLFOCUS
void ClistcomboxDlg::OnKillfocusCombo1()
{
// TODO: Add your control notification handler code here
POINT pt;
GetCursorPos(&pt);
m_listctrl.ScreenToClient(&pt);
CRect rect;
m_listctrl.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);
if(!rect.PtInRect(pt))//如果单击在一个节点文本区域内
{
CString text;
m_Cmb.GetWindowText(text);
m_listctrl.SetItemText(m_row,m_col,text);
m_Cmb.ShowWindow(SW_HIDE);//将组合框隐藏
}
}
3.3 编辑框添加失去焦点事件 EN_KILLFOCUS
void ClistcomboxDlg::OnKillfocusEdit1()
{
// TODO: Add your control notification handler code here
POINT pt;
GetCursorPos(&pt);
m_listctrl.ScreenToClient(&pt);
CRect rect;
m_listctrl.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);
if(!rect.PtInRect(pt))//如果单击在一个节点文本区域内
{
CString text;
m_Edit.GetWindowText(text);
m_listctrl.SetItemText(m_row,m_col,text);
m_Edit.ShowWindow(SW_HIDE);//将组合框隐藏
}
}
3.4 为组合框添加回车事件,即 t 处理对话框虚函数 PreTranslateMessage
BOOL ClistcomboxDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->wParam==VK_RETURN)
{
//int i,j;
CString text;
m_Cmb.GetWindowText(text);
m_listctrl.SetItemText(m_row,m_col,text);
m_Cmb.ShowWindow(SW_HIDE);//隐藏组合框
return TRUE;//阻止了父类对消息的处理,使得按下回车键时,不会关闭对话框
}
return CDialogEx::PreTranslateMessage(pMsg);
}
发现的问题行高小于编辑框,文本上下不居中:
方法:用一个空白 Image 撑起来
// TODO: 在此添加额外的初始化代码
CImageList m_l;
m_l.Create(1,21,TRUE|ILC_COLOR32,1,0); //21 正好适合组合框,这个值充当行高
m_listctrl.SetImageList(&m_l,LVSIL_SMALL);