只有在右击节点时才会触发
private void treeList1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
treeList1.ContextMenuStrip = null;
TreeListHitInfo hInfo = treeList1.CalcHitInfo(new Point(e.X, e.Y));
TreeListNode node = hInfo.Node;
treeList1.FocusedNode = node;
if (node!=null)
{
treeList1.ContextMenuStrip = contextMenuStrip1;
}
}
}
添加BarManager和popupMenu组建:
设置popupMenu组件的Manager属性:

右键点击popupMenu主键,点击Customize选项添加菜单:


然后便可添加到鼠标事件中,此处以TreeList为例:
- privatevoidtreeList1_MouseUp(objectsender,MouseEventArgse)
- {
- TreeListtree=senderasTreeList;
- if(e.Button==MouseButtons.Right
- &&ModifierKeys==Keys.None
- &&treeList1.State==TreeListState.Regular)
- {
- Pointp=newPoint(Cursor.Position.X,Cursor.Position.Y);
- TreeListHitInfohitInfo=tree.CalcHitInfo(e.Location);
- if(hitInfo.HitInfoType==HitInfoType.Cell)
- {
- tree.SetFocusedNode(hitInfo.Node);
- }
- if(tree.FocusedNode!=null)
- {
- popupMenu1.ShowPopup(p);
- }
- }
- }
本文介绍了如何在 C# 应用中通过鼠标事件触发上下文菜单的操作,包括如何实现右键点击响应、设置菜单项以及与 TreeList 控件的集成。通过 BarManager 和 popupMenu 组件的结合使用,实现了一种灵活的用户交互方式。



8536

被折叠的 条评论
为什么被折叠?



