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

时间:2010-08-23 07:56:55

标签: c# winforms contextmenu notifyicon

我有一个示例C#窗体here。我需要在鼠标左键单击时显示通知图标的上下文菜单。我已经在下面标记了所需代码的编写位置:

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

请帮忙!

1 个答案:

答案 0 :(得分:5)

在左键单击图标时显示菜单

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);
    }
}

单击问题中的按钮时显示菜单

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);
}