鼠标右键单击问题

wwx321 2003-08-20 06:53:35
在CListView视图中,单击鼠标右键,希望选中一列表项目后,弹出鼠标右键,可
总实现不了。请朋友们帮助一下:
我的代码:
void CRomateCtlView::OnRButtonDown(UINT nFlags, CPoint point)
{
UINT fFlags;

ClientToScreen(&point);
int iItem = m_list->HitTest(point, &fFlags);
CMenu menu1;
menu1.LoadMenu(IDR_POPUP);

if (fFlags & (LVHT_ONITEMLABEL | LVHT_ONITEMICON))
{
m_list->SetItemState(iItem,LVIS_SELECTED,LVIF_STATE);//此句实现不了选中该项功能

if(m_ServerSwitch==SERVER_ON)
(menu1.GetSubMenu(0))->TrackPopupMenu( TPM_LEFTALIGN ,
point.x, point.y, GetParent() );
else
(menu1.GetSubMenu(1))->TrackPopupMenu( TPM_LEFTALIGN , point.x, point.y, GetParent() );

}else
(menu1.GetSubMenu(2))->TrackPopupMenu( TPM_LEFTALIGN , point.x, point.y, GetParent() );

menu1.DestroyMenu();

}
...全文
104 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
heweixing_77 2003-08-21
  • 打赏
  • 举报
回复
DWORD pos = GetMessagePos();
CPoint pt(LOWORD(pos), HIWORD(pos));
ScreenToClient(&pt);
UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
int index = GetTopIndex();
int last_visible_index = index + GetCountPerPage();
if (last_visible_index > GetItemCount())
last_visible_index = GetItemCount();

// Loop until number visible items has been reached.
while (index <= last_visible_index)
{
CRect r;
GetItemRect(index, &r, LVIR_BOUNDS);
if (r.PtInRect(pt))
{
SetItemState(index, flag, flag);
.......
break;
}
index++;
}

试试。

wwx321 2003-08-21
  • 打赏
  • 举报
回复
soso82(海阔天空) :
我是想在listView中,你写的一些函数是treeview中的函数,所以在listview中不能用。
我要实现的功能是:
只要我在listview中单击鼠标右键,如果鼠标位置此时正好在listview的某一项上,那么
首先选中该项(使该项高亮选中,就象在此项上单击鼠标左键选中一样),选中后,弹出快捷
菜单。既是:第1步,先选中项,第2步,弹出菜单。
我不知怎样在第2步之前实现第1步????。

麻烦你,再次指教!!
指教后即揭贴。
wwx321 2003-08-21
  • 打赏
  • 举报
回复
canoe_eyes(阿里):
你的方法行得通,多谢你!
你能告诉我怎么能使整行项目选中吗?
canoe_eyes 2003-08-21
  • 打赏
  • 举报
回复
可以!
canoe_eyes 2003-08-21
  • 打赏
  • 举报
回复
DWORD pos = GetMessagePos();
CPoint pt(LOWORD(pos), HIWORD(pos));
ScreenToClient(&pt);
UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
int index = GetTopIndex();
int last_visible_index = index + GetCountPerPage();
if (last_visible_index > GetItemCount())
last_visible_index = GetItemCount();

// Loop until number visible items has been reached.
while (index <= last_visible_index)
{
CRect r;
GetItemRect(index, &r, LVIR_BOUNDS);
if (r.PtInRect(pt))
{
SetItemState(index, flag, flag);
.......
break;
}
index++;
}


goodb 2003-08-21
  • 打赏
  • 举报
回复
试一下oncontextmenu()事件。
soso82 2003-08-20
  • 打赏
  • 举报
回复
CPoint pt(::GetMessagePos());//得到本次事件中光标的位置
ScreenToClient(&pt);
UINT ClickLocation;
HTREEITEM hItem = m_Tree.HitTest(pt, &ClickLocation);
if ((hItem != NULL) && (ClickLocation & TVHT_ONITEM))
{
m_Tree.Select(hItem,TVGN_CARET);
........
}
这样就可以实现你所需的功能了!
syscookies 2003-08-20
  • 打赏
  • 举报
回复

TrackPopupMenu 这个函数的第一个参数应该设置成 TPM_LEFTALIGN | TPM_LEFTBUTTON
让这个弹出菜单知道捕捉什么鼠标事件

15,980

社区成员

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

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