简体   繁体   English

单击通知图标时如何以编程方式显示上下文菜单?

[英]How to show the context menu programmatically when click on notification icon?

I have a sample C# windows form here . 在这里有一个示例C#窗体。 I need to show the notification icon's context menu when it is left-mouse clicked. 我需要在鼠标左键单击时显示通知图标的上下文菜单。 I have marked where to write the needed codes as below: 我已经在下面标记了所需代码的编写位置:

private void button1_Click(object sender, EventArgs e)
{
        //Need to show the context menu here
}

Please help! 请帮忙!

To show the menu when your icon is left-clicked 在左键单击图标时显示菜单

private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        MethodInfo methodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu",
            BindingFlags.Instance | BindingFlags.NonPublic);

        methodInfo.Invoke(this.notifyIcon, null);
    }
}

To show the menu when the button in your question is clicked 单击问题中的按钮时显示菜单

private void button1_Click(object sender, EventArgs e)
{
    //Need to show the context menu here
    MethodInfo methodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu",
        BindingFlags.Instance | BindingFlags.NonPublic);
    methodInfo.Invoke(this.notifyIcon, null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM