关于c#:如何在系统任务栏上下文菜单中使用Windows外观?

How can I use the Windows look'n'feel for a system tray context menu?

我正在使用NotifyIconContextMenuStrip,我不想使用默认控件的外观,而默认控件的外观与Windows控件不同(现为Vista) ),方法是使用contextMenu.RenderMode = ToolStripRenderMode.ManagerRenderModecontextMenu.RenderMode = ToolStripRenderMode.Professional

alt

alt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Windows.Forms;
using System.Drawing;

public class AC : ApplicationContext
{
    NotifyIcon ni;
    public void menu_Quit(Object sender, EventArgs args)
    {
        ni.Dispose();
        ExitThread();
    }
    public AC()
    {
        ni = new NotifyIcon();
        ni.Icon = SystemIcons.Information;
        ContextMenu menu = new ContextMenu();
        menu.MenuItems.Add("Quit", new EventHandler(menu_Quit));
        ni.ContextMenu = menu;
        ni.Visible = true;
    }
    public static void Main(string[] args)
    {
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        AC ac = new AC();
        Application.Run(ac);        
    }
}


作为参考,这是如何使用Windows样式的上下文菜单获取通知图标的方法:

1)在控件工具箱中,右键单击"所有Windows窗体">"选择项"

2)勾选ContextMenu>确定

3)右键单击"所有Windows窗体">"按字母顺序对项目排序"

4)将新的ContextMenu拖动到表单设计器中

5)右键单击ContextMenu1>"属性">,将其重命名为令人难忘的名称(为将来参考,我将我的重命名为myNotifyIconContext)

6)在解决方案资源管理器中,展开Form1.cs(等)>双击Form1.Designer.cs

7)找到您的通知图标-它看起来像这样:

1
2
3
4
5
6
7
//
// notifyIcon1
//
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text ="My Notification Icon";
this.notifyIcon1.Visible = true;

在这里,我以前有一个ContextMenuStrip。

8)将第一行代码更改为(或添加):

1
this.notifyIcon1.ContextMenu = this.myNotifyIconContext;

et voila!

enter