ListCtrl控件,如何知道单击的是哪一列

ken16 2001-04-20 01:45:00
在vc中,用ListCtrl控件,当单击其中某一列是,如何知道单击的是哪一列?
就像windows2000中的任务管理器中,单击其中某一列可以排序。
第一个回答正确的给分。
...全文
111 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken16 2001-05-09
  • 打赏
  • 举报
回复
vc中没有自带的函数可以实现这种功能吗?
panda_w 2001-04-20
  • 打赏
  • 举报
回复
Detecting column index of the item clicked

--------------------------------------------------------------------------------
This article was contributed by Zafir Anjum.
The HitTest() function provided by the CListCtrl class returns only the row index of the item that falls under the point being tested. The HitTest() also has the drawback that it fails if the point is not directly over the first column. To determine the row of the item, we use GetItemRect() to get the rectangular bounds of each visible row and use PtInRect() to determine if the point falls within this rectangle. Once we know the row, we use GetColumnWidth() to determine the bounding rect for each cell and then test the point against each of these bounding rectangles.

// HitTestEx - Determine the row index and column index for a point
// Returns - the row index or -1 if point is not over a row
// point - point to be tested.
// col - to hold the column index
int CMyListCtrl::HitTestEx(CPoint &point, int *col) const
{
int colnum = 0;
int row = HitTest( point, NULL );

if( col ) *col = 0;

// Make sure that the ListView is in LVS_REPORT
if( (GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT )
return row;

// Get the top and bottom row visible
row = GetTopIndex();
int bottom = row + GetCountPerPage();
if( bottom > GetItemCount() )
bottom = GetItemCount();

// Get the number of columns
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();

// Loop through the visible rows
for( ;row <= bottom;row++)
{
// Get bounding rect of item and check whether point falls in it.
CRect rect;
GetItemRect( row, &rect, LVIR_BOUNDS );
if( rect.PtInRect(point) )
{
// Now find the column
for( colnum = 0; colnum < nColumnCount; colnum++ )
{
int colwidth = GetColumnWidth(colnum);
if( point.x >= rect.left
&& point.x <= (rect.left + colwidth ) )
{
if( col ) *col = colnum;
return row;
}
rect.left += colwidth;
}
}
}
return -1;
}


Date Last Updated: April 3, 1999
eggplant 2001-04-20
  • 打赏
  • 举报
回复
首先计算出每列的长度,宽度,即使用CRect来定位一个列,根据鼠标单击的位置,即Point是否落在CRect中,就可以判定单击哪一列了。要用CListCtrl和CHeaderCtrl两个类,源码在程序员大本营2000中VC源码,查看有关CListCtrl的即可。
feelinn 2001-04-20
  • 打赏
  • 举报
回复
我不要分了。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧