右键单击系统托盘中的通知图标不显示上下文菜单
Right clicking notifyicon in system tray does not show contextmenu
我正在尝试将上下文菜单条与通知图标一起使用。当我右键单击通知图标时,上下文菜单条不显示。我还没有在网上找到任何解决方案。
这是我的代码:
public class Program
{
static ContextMenuStrip contextMenuStrip1;
static ToolStripMenuItem exitToolStripMenuItem;
static ToolStripMenuItem restoreToolStripMenuItem;
static IContainer components;
static NotifyIcon notifyIcon1;
static win window = new win();
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Intialise();
Application.Run();
}
static void Intialise()
{
components = new Container();
contextMenuStrip1 = new ContextMenuStrip(components);
restoreToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem = new ToolStripMenuItem();
notifyIcon1 = new NotifyIcon(components);
contextMenuStrip1.SuspendLayout();
//
// restoreToolStripMenuItem
//
restoreToolStripMenuItem.Name = "restoreToolStripMenuItem";
restoreToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
restoreToolStripMenuItem.Text = "Restore";
restoreToolStripMenuItem.Click += new EventHandler(RestoreToolStripMenuItem_Click);
//
// exitToolStripMenuItem
//
exitToolStripMenuItem.Name = "exitToolStripMenuItem";
exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
exitToolStripMenuItem.Text = "Exit";
exitToolStripMenuItem.Click += new EventHandler(ExitToolStripMenuItem_Click);
//
// contextMenuStrip1
//
contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
restoreToolStripMenuItem,
exitToolStripMenuItem});
contextMenuStrip1.Name = "contextMenuStrip1";
contextMenuStrip1.RenderMode = ToolStripRenderMode.System;
contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
contextMenuStrip1.Text = "File";
//
// notifyIcon1
//
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
notifyIcon1.Icon = Properties.Resources.stm2;
notifyIcon1.Text = "Screen Time Monitor";
notifyIcon1.Visible = true;
notifyIcon1.MouseUp += new MouseEventHandler(NotifyIcon1_MouseUp);
notifyIcon1.MouseDoubleClick += new MouseEventHandler(NotifyIcon1_MouseDoubleClick);
}
private static void NotifyIcon1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
else if(e.Button == MouseButtons.Right)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
}
}
我试过使用 mouseUp 事件打开上下文菜单,但上下文菜单也不显示。我已经看到人们写 notifyIcon1.ContextMenuStrip = contextMenuStrip1;
的代码并且有效。虽然它对我不起作用。
谢谢
第 contextMenuStrip1.SuspendLayout();
行阻止了上下文菜单条的显示。我从生成的 windows 表单中复制了代码,所以我不知道为什么会有那行代码。
我正在尝试将上下文菜单条与通知图标一起使用。当我右键单击通知图标时,上下文菜单条不显示。我还没有在网上找到任何解决方案。 这是我的代码:
public class Program
{
static ContextMenuStrip contextMenuStrip1;
static ToolStripMenuItem exitToolStripMenuItem;
static ToolStripMenuItem restoreToolStripMenuItem;
static IContainer components;
static NotifyIcon notifyIcon1;
static win window = new win();
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Intialise();
Application.Run();
}
static void Intialise()
{
components = new Container();
contextMenuStrip1 = new ContextMenuStrip(components);
restoreToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem = new ToolStripMenuItem();
notifyIcon1 = new NotifyIcon(components);
contextMenuStrip1.SuspendLayout();
//
// restoreToolStripMenuItem
//
restoreToolStripMenuItem.Name = "restoreToolStripMenuItem";
restoreToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
restoreToolStripMenuItem.Text = "Restore";
restoreToolStripMenuItem.Click += new EventHandler(RestoreToolStripMenuItem_Click);
//
// exitToolStripMenuItem
//
exitToolStripMenuItem.Name = "exitToolStripMenuItem";
exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
exitToolStripMenuItem.Text = "Exit";
exitToolStripMenuItem.Click += new EventHandler(ExitToolStripMenuItem_Click);
//
// contextMenuStrip1
//
contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
restoreToolStripMenuItem,
exitToolStripMenuItem});
contextMenuStrip1.Name = "contextMenuStrip1";
contextMenuStrip1.RenderMode = ToolStripRenderMode.System;
contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
contextMenuStrip1.Text = "File";
//
// notifyIcon1
//
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
notifyIcon1.Icon = Properties.Resources.stm2;
notifyIcon1.Text = "Screen Time Monitor";
notifyIcon1.Visible = true;
notifyIcon1.MouseUp += new MouseEventHandler(NotifyIcon1_MouseUp);
notifyIcon1.MouseDoubleClick += new MouseEventHandler(NotifyIcon1_MouseDoubleClick);
}
private static void NotifyIcon1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
else if(e.Button == MouseButtons.Right)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
}
}
我试过使用 mouseUp 事件打开上下文菜单,但上下文菜单也不显示。我已经看到人们写 notifyIcon1.ContextMenuStrip = contextMenuStrip1;
的代码并且有效。虽然它对我不起作用。
谢谢
第 contextMenuStrip1.SuspendLayout();
行阻止了上下文菜单条的显示。我从生成的 windows 表单中复制了代码,所以我不知道为什么会有那行代码。