如何在系统托盘上下文菜单中使用Windows look'n'feel?

时间:2009-04-05 05:20:07

标签: c# .net winforms

我正在使用NotifyIconContextMenuStrip,我不想使用默认菜单外观和感觉哪个是开箱即用的,这个控件与Windows不同(在我的案例中使用contextMenu.RenderMode = ToolStripRenderMode.ManagerRenderModecontextMenu.RenderMode = ToolStripRenderMode.Professional

alt text

我不想使用contextMenu.RenderMode = ToolStripRenderMode.System

alt text

我只想使用标准的,正常的Windows“外观和感觉”,如无数,可能是非网络应用程序* grumble *:

alt text alt text

任何有关如何实现此目标的想法?

2 个答案:

答案 0 :(得分:4)

在Vista上,当使用所有默认值时,它会正确渲染(即,与我的机器上的DropBox渲染方式相同)。

这是一个适合我的示例程序。尝试一下,如果它没有正确呈现给你,请尝试取消注释两条注释行。

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

答案 1 :(得分:0)

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

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

2)勾选ContextMenu>行

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

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

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

6)在Solution Explorer中,展开Form1.cs(etc)>双击Form1.Designer.cs

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)将第一行代码更改为(或添加):

this.notifyIcon1.ContextMenu = this.myNotifyIconContext;

et voila!

enter image description here