1、在dlg窗体添加一个CListCtrl 和一个CEdit
2、为此两控件对应绑定两成员变量,如下
CListCtrl m_List;
CEdit m_cTextEdit;
3、在dlg.h头文件添加两似有成员变量
private:
int m_nRow;
int m_nCol;
4、在oninitdialog初始化化listctrl 和edit
// 设置接收文件列表风格
LONG lStyle;
lStyle = GetWindowLong(m_List.m_hWnd, GWL_STYLE);
lStyle |= LVS_REPORT;
SetWindowLong(m_List.m_hWnd, GWL_STYLE, lStyle);
DWORD dwStyle = m_List.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
dwStyle |= LVS_EX_GRIDLINES;
m_List.SetExtendedStyle(dwStyle);
// 初始化列表头信息
m_List.InsertColumn( em_LIST_ITEM_KEY, "Key", LVCFMT_LEFT, 140 );
m_List.InsertColumn( em_LIST_ITEM_VALUE, "Value", LVCFMT_LEFT, 200 );
m_cTextEdit.ShowWindow( SW_HIDE );
5、为CListCtrl添加双击事件处理
void CXXXDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
char ach[MAX_LISTITME_LEN] = {0};
CRect cRectItem;
CRect cRectList;
NM_LISTVIEW* pNMListView = ( NM_LISTVIEW* )pNMHDR;
if ( -1 != pNMListView->iItem )
{
m_nRow = pNMListView->iItem;
m_nCol = pNMListView->iSubItem;
//过滤key,key不允许修改
if ( 0 == m_nCol)
{
*pResult = 0;
return;
}
m_List.GetSubItemRect( pNMListView->iItem, pNMListView->iSubItem, LVIR_LABEL, cRectItem );
m_List.GetWindowRect( &cRectList );
ScreenToClient( &cRectList );
cRectItem.left += cRectList.left + 3;
cRectItem.top += cRectList.top;
cRectItem.right += cRectList.left + 3;
cRectItem.bottom += cRectList.top + 3;
m_List.GetItemText( pNMListView->iItem, pNMListView->iSubItem, ach, MAX_LISTITME_LEN);
m_cTextEdit.SetWindowText( ach );
m_cTextEdit.ShowWindow( SW_SHOW );
m_cTextEdit.MoveWindow( &cRectItem );
m_cTextEdit.SetFocus();
m_cTextEdit.CreateSolidCaret( 1, cRectItem.Height() - 5 );
m_cTextEdit.ShowCaret();
m_cTextEdit.SetSel( -1 );
}
*pResult = 0;
}
6、为m_cTextEdit添加光标消失事件处理
void CXXXDlg::OnKillfocusEditText()
{
// TODO: Add your control notification handler code here
CString cstrText = "";
m_cTextEdit.GetWindowText( cstrText );
m_List.SetItemText( m_nRow, m_nCol, cstrText );
m_cTextEdit.ShowWindow(SW_HIDE);
}
7、结束.