首页 > 解决方案 > 右键单击系统托盘中的通知图标不显示上下文菜单

问题描述

我正在尝试使用带有通知图标的上下文菜单条。当我右键单击通知图标时,上下文菜单条不显示。我在网上没有找到任何解决方案。这是我的代码:

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;并且有效。虽然它对我不起作用。谢谢。

标签: c#notifyiconcontextmenustrip

解决方案


该行contextMenuStrip1.SuspendLayout();停止显示上下文菜单条。我从生成的 windows 窗体中复制了代码,所以我不知道为什么那行代码在那里。


推荐阅读