Правый клик по уведомлению в системном трее не показывает контекстное меню - PullRequest
0 голосов
/ 09 февраля 2019

Я пытаюсь использовать 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 голосов
/ 12 февраля 2019

Строка contextMenuStrip1.SuspendLayout(); остановила показ контекстной меню.Я скопировал код из сгенерированной формы Windows, поэтому я не знаю, почему эта строка кода была там.

...