1.定義成員變量:
CComboBox m_Cmb; 、、將它與組合框控件關聯,
CEdit m_Edit;、、將它與編輯框控件關聯,
int m_row,m_col; //記錄用戶點擊的那個單元格所在的行與列號
2.添加listctrl的單擊響應消息主要是完成了單擊後將控件顯示出來。添加代碼如下
void CControllerDialogAdd::OnClickListDoor(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
LPNMLISTVIEW pNMTreeView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: 在此添加控件通知處理程序代碼
POINT PT;
GetCursorPos(&PT);
m_DoorList.ScreenToClient(&PT);
LVHITTESTINFO hitInfo;
hitInfo.pt=PT;
//hitem=m_Tree.GetSelectedItem();
m_DoorList.SubItemHitTest(&hitInfo);
if(hitInfo.flags & LVHT_ONITEMLABEL)//判斷是否單擊在文本上
{
CRect rect;
// m_DoorList.GetSubItemRect(hitInfo.iItem,hitInfo.iSubItem,LVIR_BOUNDS,rect);
m_DoorList.GetSubItemRect(hitInfo.iItem,hitInfo.iSubItem,LVIR_BOUNDS,rect);
if (hitInfo.iSubItem==0)
{
rect.right=rect.left+m_DoorList.GetColumnWidth(0);
}
else if(hitInfo.iSubItem==1)//對列進行判斷在這裏我是1,2爲edit,3,4爲combo box
{
CString mes=m_DoorList.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_DoorList.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_DoorList.GetItemText(hitInfo.iItem,hitInfo.iSubItem);
// rect.InflateRect(0,0,0,0);//增大組合框的高度使其可以容納整行文本。
m_col=hitInfo.iSubItem;
m_row=hitInfo.iItem;
m_ComAct.MoveWindow(&rect,TRUE);
m_ComAct.ShowWindow(SW_NORMAL);
m_ComAct.SetWindowText(mes);
m_ComAct.BringWindowToTop();
m_ComAct.SetFocus();//使組合框聚焦
}
else if (hitInfo.iSubItem==4)
{
CString mes=m_DoorList.GetItemText(hitInfo.iItem,hitInfo.iSubItem);
// rect.InflateRect(0,0,0,0);//增大組合框的高度使其可以容納整行文本。
m_col=hitInfo.iSubItem;
m_row=hitInfo.iItem;
m_ControlType.MoveWindow(&rect,TRUE);
m_ControlType.ShowWindow(SW_NORMAL);
m_ControlType.SetWindowText(mes);
m_ControlType.BringWindowToTop();
m_ControlType.SetFocus();//使組合框聚焦
}
*pResult = 0;
}
}
3.對話框初始化時,添加如下代碼:
m_Cmb.SetParent(&m_List); //確保CComboBox的座標是相對於列表控件而言的。
m_Edit.SetParent(&m_List);//確保Edit控件的座標是相對於列表控件而言的。
4.當用戶鼠標離開CComboBox控件時,設置單元格文本,並隱藏CComboBox控件
當用戶鼠標離開Edit控件時,設置單元格文本,並隱藏Edit控件
首先我們要添加CComboBox控件的CBN_KILLFOCUS消息和Edit控件的EN_KILLFOCUS消息
添加代碼如下:
void CControllerDialogAdd::OnKillfocusComboActive()
{
// TODO: 在此添加控件通知處理程序代碼
POINT pt;
// int i,j;
GetCursorPos(&pt);
m_DoorList.ScreenToClient(&pt);
CRect rect;
m_DoorList.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);
if(!rect.PtInRect(pt))//如果單擊在一個節點文本區域內
{
CString text;
m_ComAct.GetWindowText(text);
m_DoorList.SetItemText(m_row,m_col,text);
m_ComAct.ShowWindow(SW_HIDE);//將組合框隱藏
}
}
void CControllerDialogAdd::OnKillfocusEdit1()
{
POINT pt;
GetCursorPos(&pt);
m_DoorList.ScreenToClient(&pt);
CRect rect;
m_DoorList.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);
if(!rect.PtInRect(pt))//如果單擊在一個節點文本區域內
{
CString text;
m_Edit.GetWindowText(text);
m_DoorList.SetItemText(m_row,m_col,text);
m_Edit.ShowWindow(SW_HIDE);//將組合框隱藏
}
}
5.當用戶在CCombobox中按下[Enter]鍵時,設置單元格文本,並隱藏CComboBox控件
-
BOOL CListDlg1Dlg::PreTranslateMessage(MSG *pMsg) { if(pMsg->wParam==VK_RETURN) { int i,j; CString text; m_Cmb.GetWindowText(text); m_List.SetItemText(m_row,m_col,text); m_Cmb.ShowWindow(SW_HIDE);//隱藏組合框 return TRUE;//阻止了父類對消息的處理,使得按下回車鍵時,不會關閉對話框 } return CDialog::PreTranslateMessage(pMsg); }