我制作了一个带有图标的托盘应用程序,该图标会在“图标点击”上打开 ContextMenuStrip 。我有三个可以点击的 ToolStripMenuItem 。
只要直接可见图标,托盘应用程序就可以正常工作。但是,如果必须单击V形以展开不可见的托盘图标,则会遇到一个问题。 我可以通过单击项目打开菜单。如果单击 ToolStripMenuItems 之一,则托盘菜单会进入“扩展托盘图标”的背景,并且无法再单击。我该如何预防? 看来背景是在捕获点击,而不是开放形式。
这是菜单的代码。
class Menu
{
private static ContextMenuStrip contextMenuStrip;
private static NotifyIcon notifyIcon1 = new NotifyIcon();
public Menu()
{
notifyIcon1.Icon = new Icon("icon.ico");
notifyIcon1.Text = "icon";
contextMenuStrip = new ContextMenuStrip();
ToolStripItem stripItem0 = new ToolStripMenuItem("open");
contextMenuStrip.Items.AddRange(new ToolStripItem[] { stripItem0 });
ToolStripItem stripItem1 = new ToolStripMenuItem("stop");
stripItem1.Click += new EventHandler(ToggleTracking);
contextMenuStrip.Items.AddRange(new ToolStripItem[] { stripItem1 });
ToolStripItem line = new ToolStripSeparator();
contextMenuStrip.Items.AddRange(new ToolStripItem[] { line });
ToolStripItem stripItem2 = new ToolStripMenuItem("close");
stripItem2.Click += new EventHandler(Close);
contextMenuStrip.Items.AddRange(new ToolStripItem[] { stripItem2 });
notifyIcon1.ContextMenuStrip = contextMenuStrip;
//notifyIcon1.ContextMenu = contextMenu1;
notifyIcon1.Click += new EventHandler(IconClick);
notifyIcon1.Visible = true;
}
private void ToggleTracking(object Sender, EventArgs e)
{
ToolStripMenuItem stripItem1 = (ToolStripMenuItem)contextMenuStrip.Items[1];
if (stripItem1.Text == "stop")
{
stripItem1.Text = "restart";
Batch.StopTimer();
}
else
{
stripItem1.Text = "stop";
Batch.RestartTimer();
}
}
private void Close(object Sender, EventArgs e)
{
Application.Exit();
}
private void IconClick(object Sender, EventArgs e)
{
Control strip = Sender as Control;
if (contextMenuStrip.Visible)
{
contextMenuStrip.Hide();
}
else
{
contextMenuStrip.Show(Cursor.Position);
}
}
}
答案 0 :(得分:1)
我必须像这样使用BringToFront方法。
{
contextMenuStrip.Show(Cursor.Position);
contextMenuStrip.BringToFront();
}
https://msdn.microsoft.com/de-de/library/system.windows.forms.control.bringtofront(v=vs.110).aspx