右键单击系统任务栏中的notifyicon不会显示上下文菜单

时间:2019-02-09 10:06:06

标签: c# notifyicon contextmenustrip

我正在尝试使用带有notifyicon的contextmenustrip。当我右键单击notifyicon时,contextmenustrip不会显示。我尚未在网络上找到任何解决方案。 这是我的代码:

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;的代码,并且可以正常工作。虽然它对我不起作用。 谢谢。

1 个答案:

答案 0 :(得分:0)

contextMenuStrip1.SuspendLayout();行阻止了contextmenustrip的显示。我已经从生成的Windows表单中复制了代码,所以我不知道为什么那行代码在那里。