得到ListCtrl所选中行的某个字段的值

时间:2022-06-11 15:05:25
我在对话框的OnInitDlg里面,将ListCtrl这样处理了。
m_listctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_GRIDLINES);

我的ListCtrl有好几列,其中第一列列名为“序号”它的值为0,1,2,3。。。递增。

1.我希望在单击或者双击的时候,得到选中行的序号值。请问如何处理。。谢谢了。

2.另外,对于某行,我左键单击,它整行显示为银白色;右键单击,却为蓝色。请问,这种高度显亮时的颜色可以控制吗?如何控制呢?。谢谢了。。

8 个解决方案

#1


自己up

#2


如果我希望得到选种行任意一个字段的值怎么办呢?。。
谢谢了~~

#3


int i;
CString str;
CRominfoset m_user;
i=m_roomlist.GetSelectionMark();
str=m_roomlist.GetItemText(i,0);//得到的值在str里,i表示选中行,0表示第一列

#4


获取ListCtrl中选中的行列号
void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);

LVHITTESTINFO info;
info.pt = pt;
info.flags = LVM_SUBITEMHITTEST ;
int i= SubItemHitTest(&info);

*pResult = 0;
}

#5


有了行列号就好操作了,你可以查以前的帖子,很多的。

#6


Retrieves the text of a list view item or subitem.

int GetItemText(
   int nItem,
   int nSubItem,
   LPTSTR lpszText,
   int nLen 
) const;
CString GetItemText(
   int nItem,
   int nSubItem 
) const;
Parameters
nItem 
The index of the item whose text is to be retrieved. 
nSubItem 
Specifies the subitem whose text is to be retrieved. 
lpszText 
Pointer to a string that is to receive the item text. 
nLen 
Length of the buffer pointed to by lpszText. 
Return Value
The version returning int returns the length of the retrieved string.

The version returning a CString returns the item text.

#7


Retrieves the selection mark of a list view control.

int GetSelectionMark( );
Return Value
The zero-based selection mark, or -1 if there is no selection mark.

#8


CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
   TRACE0("No items were selected!\n");
else
{
   while (pos)
   {
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here
   }
}

nTtem为你选中的行号。

#1


自己up

#2


如果我希望得到选种行任意一个字段的值怎么办呢?。。
谢谢了~~

#3


int i;
CString str;
CRominfoset m_user;
i=m_roomlist.GetSelectionMark();
str=m_roomlist.GetItemText(i,0);//得到的值在str里,i表示选中行,0表示第一列

#4


获取ListCtrl中选中的行列号
void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);

LVHITTESTINFO info;
info.pt = pt;
info.flags = LVM_SUBITEMHITTEST ;
int i= SubItemHitTest(&info);

*pResult = 0;
}

#5


有了行列号就好操作了,你可以查以前的帖子,很多的。

#6


Retrieves the text of a list view item or subitem.

int GetItemText(
   int nItem,
   int nSubItem,
   LPTSTR lpszText,
   int nLen 
) const;
CString GetItemText(
   int nItem,
   int nSubItem 
) const;
Parameters
nItem 
The index of the item whose text is to be retrieved. 
nSubItem 
Specifies the subitem whose text is to be retrieved. 
lpszText 
Pointer to a string that is to receive the item text. 
nLen 
Length of the buffer pointed to by lpszText. 
Return Value
The version returning int returns the length of the retrieved string.

The version returning a CString returns the item text.

#7


Retrieves the selection mark of a list view control.

int GetSelectionMark( );
Return Value
The zero-based selection mark, or -1 if there is no selection mark.

#8


CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
   TRACE0("No items were selected!\n");
else
{
   while (pos)
   {
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here
   }
}

nTtem为你选中的行号。