ListCtrl:如何得知用户双击的是哪一个SubItem?

ExitWindows 2003-09-29 10:58:23
MFC中,重载CListCtrl类,响应其双击消息,函数为OnDblclk(NMHDR* pNMHDR, LRESULT* pResult),如何得知用户点击的是哪一行的哪一列呢?
...全文
134 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
daylight1980 2003-09-29
  • 打赏
  • 举报
回复
以上我的方法为最简单最好的,楼主实验一下吧
daylight1980 2003-09-29
  • 打赏
  • 举报
回复
不要忘了设置属性
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_FLATSB |LVS_EX_GRIDLINES );
daylight1980 2003-09-29
  • 打赏
  • 举报
回复
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;
//get the row number
int nItem = temp->iItem;
//get the column number
int nSubItem = temp->iSubItem;
flyelf 2003-09-29
  • 打赏
  • 举报
回复
Cxxx::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY*)pNMHDR;
phdn->iButton就是当前双击的列的编号,0为第一列
*pResult = 0;
}
ExitWindows 2003-09-29
  • 打赏
  • 举报
回复
谢谢.我忘了怎么取得pt,能不能再指点一下?
sunyuan_01 2003-09-29
  • 打赏
  • 举报
回复
给个代码.你看看吧,
if (!EnsureVisible (Item, TRUE))
{
InsertItemEx (Item);
if (!EnsureVisible (Item, TRUE))
return NULL;
}

// Make sure that nCol is valid
CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
if (Column >= nColumnCount || GetColumnWidth (Column) < 5)
return NULL;

// Get the column offset
int Offset = 0;
for (int iColumn = 0; iColumn < Column; iColumn++)
Offset += GetColumnWidth (iColumn);

CRect Rect;
GetItemRect (Item, &Rect, LVIR_BOUNDS);

// Now scroll if we need to expose the column
CRect ClientRect;
GetClientRect (&ClientRect);
if (Offset + Rect.left < 0 || Offset + Rect.left > ClientRect.right)
{
CSize Size;
if (Offset + Rect.left > 0)
Size.cx = -(Offset - Rect.left);
else
Size.cx = Offset - Rect.left;
Size.cy = 0;
Scroll (Size);
Rect.left -= Size.cx;
}
sunyuan_01 2003-09-29
  • 打赏
  • 举报
回复
在OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)中处理

int nItemIndex = -1; //列表中被选行的索引

if((nItemIndex = m_ListCtrl.GetNextItem(-1,LVNI_SELECTED))!=-1)
{
....
}

列,可能你自己要处理
ExitWindows 2003-09-29
  • 打赏
  • 举报
回复
我就是想知道是哪一列啊,怎样自已判断呢?
flyelf 2003-09-29
  • 打赏
  • 举报
回复
不好意思,刚才没有看清楚,在CListCtrl中可以使用HitTest来得到是那一行那一列
CListCtrl.HitTest(LVHITTESTINFO* pHitTestInfo)
LVHITTESTINFO


Has been extended to accommodate subitem hit-testing. The LVHITTESTINFO structure contains information about a hit test. It is used in association with the LVM_HITTEST and LVM_SUBITEMHITTEST messages and their related macros. This structure supersedes the LV_HITTESTINFO structure.

typedef struct _LVHITTESTINFO {
POINT pt;
UINT flags;
int iItem;
int iSubItem;
} LVHITTESTINFO, FAR *LPLVHITTESTINFO;

Members
pt
Position to hit test, in client coordinates.
flags
Variable that receives information about the results of a hit test. This member can be one or more of the following values: LVHT_ABOVE The position is above the control's client area.
LVHT_BELOW The position is below the control's client area.
LVHT_NOWHERE The position is inside the list view control's client window, but it is not over a list item.
LVHT_ONITEMICON The position is over a list view item's icon.
LVHT_ONITEMLABEL The position is over a list view item's text.
LVHT_ONITEMSTATEICON The position is over the state image of a list view item.
LVHT_TOLEFT The position is to the left of the list view control's client area.
LVHT_TORIGHT The position is to the right of the list view control's client area.

You can use LVHT_ABOVE, LVHT_BELOW, LVHT_TOLEFT, and LVHT_TORIGHT to determine whether to scroll the contents of a list view control. Two of these values may be combined. For example, if the position is above and to the left of the client area, you could use both LVHT_ABOVE and LVHT_TOLEFT.

You can test for LVHT_ONITEM to determine whether a specified position is over a list view item. This value is a bitwise-OR operation on LVHT_ONITEMICON, LVHT_ONITEMLABEL, and LVHT_ONITEMSTATEICON.

iItem
Receives the index of the matching item. Or if hit-testing a subitem, this value represents the subitem's parent item.
iSubItem
Version 4.70. Receives the index of the matching subitem. When hit-testing an item, this member will be zero.
flyelf 2003-09-29
  • 打赏
  • 举报
回复
使用GetCurSel()可以得到是那一行,至于那一列,就需要自己判断了
ExitWindows 2003-09-29
  • 打赏
  • 举报
回复
请指教.
ExitWindows 2003-09-29
  • 打赏
  • 举报
回复
谢谢各位.
只是 flyelf(空谷清音)老兄的代码虽可运行(win2000)但MSDN说需winCE.
hardstudylulin 2003-09-29
  • 打赏
  • 举报
回复
//使用GetCurSel()可以得到是那一行,
//用下面的函数可以得到点击的列
int
gxListCtrl::HitTestEx (CPoint& Point, int* pColumn)//返回所点击的列数
{
int ColumnNum = 0;
int Row = HitTest (Point, NULL);

if (pColumn)
*pColumn = 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 (ColumnNum = 0; ColumnNum < nColumnCount; ColumnNum++)
{
int ColWidth = GetColumnWidth (ColumnNum);
if (Point.x >= Rect.left && Point.x <= (Rect.left + ColWidth))
{
if (pColumn)
*pColumn = ColumnNum;
return Row;
}
Rect.left += ColWidth;
}
}
}
return -1;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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