非常感谢!原来还有个LVM_GETSUBITEMRECT消息。 我查了一下MSDN,对它的一个参数有点不清楚,就是那个接受位置的RECT结构: lpRect Long pointer to a RECT structure that receives the subitem bounding rectangle data. The members of RECT must be initialized according to the member and value relationships shown in the following tables. Member Value top The one-based index of the subitem. left Flag value (see following). Indicates the portion of the list-view subitem for which to retrieve the bounding rectangle. Value Description LVIR_BOUNDS Returns the bounding rectangle of the entire item, including the icon and label. LVIR_ICON Returns the bounding rectangle of the icon or small icon. LVIR_LABEL Returns the bounding rectangle of the entire item, including the icon and label. This is identical to LVIR_BOUNDS. 上面的内容似乎是说在发送这个消息的时候,RECT结构的TOP字段必须填上子项目索引,LEFT字段必须填上标志,这个标志只可能是LVIR_BOUNDS,LVIR_ICON和LVIR_LABEL三种,而第一种和第三种完全相同,返回RECT的内容是"entire item“的边界。请问这个entire item的边界,是指整个项目(包括主项目和所有子项目)的边界,还是指这一个子项目的所有的边界(包括图标和标签)?
/****************************************************************************
1.在ListView类的OnInitialUpdate里面添加LVS_EDITLABELS属性 void CEditListViewView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& m_ListCtrl=GetListCtrl();
//设置基本样式
LONG lStyle;
lStyle=GetWindowLong(m_ListCtrl.m_hWnd, GWL_STYLE); //获取当前窗口style
lStyle |=LVS_REPORT ; //以详细资料即报告方式显示;
lStyle |=LVS_SHOWSELALWAYS ; //一直显示被选中表项方式;
lStyle |=LVS_EDITLABELS ; //可编辑;
SetWindowLong(m_ListCtrl.m_hWnd, GWL_STYLE, lStyle);//设置style 。
}
2.在Frame类里添加虚函数:DefWindowProc,和以下代码: LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message==WM_NOTIFY)
{
LPNMHDR pnmh =(LPNMHDR) lParam;
if (pnmh->code==LVN_ENDLABELEDIT)
{ CListCtrl& m_ListCtrl=((CListView*)GetActiveView())->GetListCtrl(); LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pnmh;
LVITEMA item = pDispInfo->item;
CString str = item.pszText;
str.TrimLeft();
str.TrimRight();
if (str.GetLength() > 0)
m_ListCtrl.SetItemText(item.iItem,item.iSubItem,item.pszText);
} }
return CFrameWnd::DefWindowProc(message, wParam, lParam);
}3.一切ok!愿顺利!阿弥陀佛!
例子代码下载:(请不要使用下载工具,否则可能出错)
http://cid-3ba16e78a53d2d3d.office.live.com/self.aspx/VC/EditListView.zip****************************************************************************/
GetWindowRect获取窗口的大小和位置,可能要调用ClientToScreen函数转换坐标;
CRect rt;
GetWindowRect(rt);
然后动态创建一个编辑框,调用SetWindowPos将它的大小设置为rt.Width和rt.Height,还有位置rt.x,rt.y;
当你输入了你的内容后,将此编辑框的内容更新到ListCtrl的“单元格”中,可以调用SetWindowText,也可以直接发消息...
LVM_GETITEMRECT
LVM_GETSUBITEMRECT
VC:
CListCtrl::GetItemRect
CListCtrl::GetSubItemRect
CListCtrl::GetItemRect和CListCtrl::GetSubItemRect
我查了一下MSDN,对它的一个参数有点不清楚,就是那个接受位置的RECT结构:
lpRect
Long pointer to a RECT structure that receives the subitem bounding rectangle data. The members of RECT must be initialized according to the member and value relationships shown in the following tables.
Member Value
top The one-based index of the subitem.
left Flag value (see following). Indicates the portion of the list-view subitem for which to retrieve the bounding rectangle. Value Description
LVIR_BOUNDS Returns the bounding rectangle of the entire item, including the icon and label.
LVIR_ICON Returns the bounding rectangle of the icon or small icon.
LVIR_LABEL Returns the bounding rectangle of the entire item, including the icon and label. This is identical to LVIR_BOUNDS. 上面的内容似乎是说在发送这个消息的时候,RECT结构的TOP字段必须填上子项目索引,LEFT字段必须填上标志,这个标志只可能是LVIR_BOUNDS,LVIR_ICON和LVIR_LABEL三种,而第一种和第三种完全相同,返回RECT的内容是"entire item“的边界。请问这个entire item的边界,是指整个项目(包括主项目和所有子项目)的边界,还是指这一个子项目的所有的边界(包括图标和标签)?
void CXXXDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NMLISTVIEW* pList = (NMLISTVIEW*)pNMHDR;
int nItem = pList->iItem;
if(nItem)
{
int nSubItem = pList->iSubItem;
CString str;
str.Format(_T("Item: %d, SubItem: %d"), nItem, nSubItem);
AfxMessageBox(str);
}
*pResult = 0;
}
{
LPNMITEMACTIVATE pItemActivate = (LPNMITEMACTIVATE)lParam;
if ( pItemActivate == NULL ) return S_OK;
int nItem = pItemActivate->iItem ;
int nSubItem = pItemActivate->iSubItem ;
看这个nSubItem
if(nItem)
--》
if(-1 != nItem)
是发给 listview的 父窗口的。
case NM_DBLCLK:
OnItemDBLCLK(lParam) ;
break;OnItemDBLCLK(LPARAM lParam)
{
LPNMITEMACTIVATE pItemActive = (LPNMITEMACTIVATE)lParam ;
_ASSERT(pItemActive);
int nItem = pItemActive->iItem ;
int nSubItem = pItemActive->iSubItem ;
if ( m_bEditable && nItem >= 0 && nSubItem >= 0 && !IsGroupItem(nItem) && TRUE == GetCellEditable(nItem,nSubItem) )
{
DWORD dwUserData = 0 ;
//Create in-place edit
CreateInPlaceEdit(nItem,nSubItem,dwUserData) ;
}
不行的话,把code upload 到 bbs.aisnote.com 我给你看看
学习中,原来在CListBox控件中实现编辑功能时,都是在响应行动态创建一个编辑控件,编辑完后再更新列表框的内容,
在CListview 中直接就有可编辑属性?回去试试
我也找了好久才找、、、