vc中CListCtrl控件点击修改数据

本文介绍了一种在CListCtrl控件中实现点击编辑功能的方法。通过代码示例展示了如何响应点击事件,并使用编辑框进行值的修改,最后更新到列表控件中。
2009-04-03 06:56 745人阅读 评论(0) 收藏 举报
网上关于在CListCtrl控件里面点击直接修改值的东西比较少,其实是个很简单的东西,知识不太容易想到!在这里将代码写下!

void CAdminDialog::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
Invalidate();
HWND hWnd1 = ::GetDlgItem(m_hWnd,IDC_LIST1);
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;
RECT rect;
//get the row number
nItem = temp->iItem;
//get the column number
nSubItem = temp->iSubItem;
if(nSubItem == 0 || nSubItem == -1 || nItem == -1)
return ;
//Retrieve the text of the selected subItem from the list
str1 = GetItemText(hWnd1,nItem ,nSubItem);
RECT rect1,rect2;
// this macro is used to retrieve the Rectanle of the selected SubItem
ListView_GetSubItemRect(hWnd1,temp->iItem,temp->iSubItem,LVIR_BOUNDS,&rect);
//Get the Rectange of the listControl
::GetWindowRect(temp->hdr.hwndFrom,&rect1);
//Get the Rectange of the Dialog
::GetWindowRect(m_hWnd,&rect2);

int x=rect1.left-rect2.left;
int y=rect1.top-rect2.top;

if(nItem != -1)
::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),HWND_TOP,rect.left + x,rect.top + y,rect.right-rect.left,rect.bottom-rect.top,NULL);
//GetDlgItem(IDC_EDIT1)->SetWindowText(str1);//°ÑEditµÄ¿Ø¼þÖµÉèΪCListCtrlÀïÃæµÄÖµ
m_List.SetItemText(nItem,nSubItem,"");//½«CListCtrlÀïÃæµÃÖµÉèÖóɲ»¿É¼ûµÄ

::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);
::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));
//Draw a Rectangle around the SubItem
::Rectangle(::GetDC(temp->hdr.hwndFrom),rect.left + x,rect.top,rect.right,rect.bottom);
GetDlgItem(IDC_EDIT1)->GetWindowText(str1);//µÃµ½Ð޸ĺóµÄÖµ£¬ÒÔ·½±ãÈ·¶¨Ð޸ĺ󽫴ËÖµÖ±½Óдµ½CListCtrlÉÏ

//Set the listItem text in the EditBox
//::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str1);

*pResult = 0;
}

CString CAdminDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
} while (nRes == nLen-1);
str.ReleaseBuffer();
return str;
}

void CAdminDialog::OnChange()
{
// TODO: Add your control notification handler code here
//::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str1);
GetDlgItem(IDC_EDIT1)->GetWindowText(str1);//µÃµ½EditÀïÃæµÄÄÚÈÝ
m_List.SetItemText(nItem,nSubItem,str1);//½«EditÀïÃæµÄֵдµ½CListCtrl¿Ø¼þÀïÃæÈ¥
//delete GetDlgItem(IDC_EDIT1);
GetDlgItem(IDC_EDIT1)->SetWindowText("");//Òª½«EditµÄÎı¾ÉèÖóɿգ¬²»È»ÔÙ´ÎÏÔʾEditµÄʱºò»áÒ²ÏÔʾÉÏ´ÎÐ޸ĵÄÖµ
GetDlgItem(IDC_EDIT1)->ShowWindow(SW_HIDE);//ÒªÒþ²Ø£¬²»ÒªÉ¾³ý£¬ÒòΪɾ³ýÒÑÓÐÔٴε¥»÷µÄʱºò¾Í»á³ö´í
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值