WPF 托盘图标右键弹出的ContextMenu如何关闭
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 托盘图标右键弹出的ContextMenu如何关闭相关的知识,希望对你有一定的参考价值。
我在托盘加入了NotifyIcon控件,在Click事件中加入了contextMenu.IsOpen=true;这样来打开右键菜单。
我想在弹出菜单后,鼠标点击其他位置时,菜单自动消失,请问怎么做?
我的系统托盘里,其他软件图标都是右键点击后,弹出菜单,点击其他位置菜单消失。
我只能实现右键点击弹出菜单,但是点击其他位置菜单不消失,怎么办?我尝试了菜单在LostFocus时context.IsOpen=false;这样也不行。
谢谢~!
System.Windows.Forms.ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip();
System.Windows.Forms.ToolStripMenuItem item1 = new System.Windows.Forms.ToolStripMenuItem();
item1.Click += new EventHandler(item1_Click);
item1.Text = "显示主面板";
System.Windows.Forms.ToolStripMenuItem item2= new System.Windows.Forms.ToolStripMenuItem();
item2.Text = "退出";
item2.Click += new EventHandler(item2_Click);
contextMenu.Items.Add(item1);
contextMenu.Items.Add(item2);
this.notifyIcon.ContextMenuStrip = contextMenu; 参考技术A LostFocus 基本没用的,WPF里面没有NotifyIcon控件,你用的,应该是从Winform里面拿过来的对吧?那样就要自己判断全局的鼠标事件,程序加载时,设置全局监听钩子.只能这样处理.
bootstrap treeview 和Table 击右键弹出菜单
本文参考了Bootstrap Styled Context Menu Plugin With jQuery 并且在该网页上下载了bootstrap-contextmenu.js.
contextmenu 实现击右键弹出子菜单。 它可以和bootstrap-treeview以及table 结合起来使用。
测试过的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>neo4j On NodeJS</title>
<meta charset="utf-8">
<title>Bootstrap 实例 - 默认的导航栏</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script src="/js/jquery-3.6.0.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/bootstrap-contextmenu.js"></script>
<script src="/js/bootstrap-treeview.js"></script>
<script>
var treeData = [
{
text: "Parent 1",
selectable: true,
state: {
checked: true,
expanded: false,
selected: true
},
nodes: [
{
text: "Child 1",
nodes: [
{
text: "Grandchild 1"
},
{
text: "Grandchild 2"
}
]
},
{
text: "Child 2"
}
]
},
{
text: "Parent 2"
},
{
text: "Parent 3"
},
{
text: "Parent 4"
},
{
text: "Parent 5"
}
];
</script>
</head>
<body>
<div class="container" style="margin-top:150px;">
<h1>Bootstrap Context Menu Plugin Demo</h1>
<h3>Treeview</h3>
<div id="tree">
</div>
<h3>Table</h3>
<table id="myTable" class="table table-inverse">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<ul id="contextMenu" class="dropdown-menu" role="menu" style="display:none">
<li class="dropdown-item"><a tabindex="-1" href="#">Action</a></li>
<li class="dropdown-item"><a tabindex="-1" href="#">Another action</a></li>
<li class="dropdown-item"><a tabindex="-1" href="#">Something else here</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-item"><a tabindex="-1" href="#">Separated link</a></li>
</ul>
<script>
function collapseNode(nodeId) {
$('#tree').treeview('collapseNode', [nodeId]);
//alert("collapse " + nodeId);
}
function expandNode(nodeId) {
$('#tree').treeview('expandNode', [nodeId]);
// alert("expand " + nodeId);
}
$('#tree').treeview({
data: treeData,
multiSelect: true,
highlightSelected: false,
onNodeSelected: function(event, node) {
if (node.state.expanded) {
console.log(node.text);
collapseNode(node.nodeId);
} else {
console.log(node.text);
expandNode(node.nodeId);
}
}
});
$("#tree").contextMenu({
menuSelector: "#contextMenu",
menuSelected: function (invokedOn, selectedMenu) {
var msg = "You selected the menu item '" + selectedMenu.text() +
"' on the value '" + invokedOn.text() + "'";
alert(msg);
}
});
$("#myTable").contextMenu({
menuSelector: "#contextMenu",
menuSelected: function (invokedOn, selectedMenu) {
var msg = "You selected the menu item '" + selectedMenu.text() +
"' on the value '" + invokedOn.text() + "'";
alert(msg);
}
});
</script>
</div>
</body>
</html>
以上是关于WPF 托盘图标右键弹出的ContextMenu如何关闭的主要内容,如果未能解决你的问题,请参考以下文章
WPF 在datagrid和ScrollViewer的滚动条上右键发现弹出的选项全是英文,怎么改成汉语?