将系统托盘notifyicon.icon设置为图像文件夹中的图片

时间:2013-06-03 18:53:21

标签: c# image notifyicon

我尝试了几件事,最后只是将图像直接放在C:\ Users \ Gebruiker \ Documents \ Visual Studio 2012 \ Projects \ FolderMonitor \ FolderMonitor \ bin \ Debug中。这个现在适用,但理想情况下我想将notifyIcon.Icon = new Icon(“folder.ico”)设置为解决方案中images文件夹内的图像。但我无法弄清楚这是如何运作的。

    public FolderMonitorApplicationContext()
    {
        this.monitor =  new Monitor();

        notifyIcon = new NotifyIcon();
        notifyIcon.Icon = new Icon("folder.ico");
        notifyIcon.Text = "Folder Monitor";
        notifyIcon.Visible = true;

        contextMenu = new ContextMenuStrip();
        openMonitor = new ToolStripMenuItem();
        exitApplication = new ToolStripMenuItem();

        notifyIcon.ContextMenuStrip = contextMenu;

        notifyIcon.DoubleClick += NotifyIcon_DoubleClick;

        openMonitor.Text = "Open";
        openMonitor.Click += new EventHandler(OpenMonitor_Click);
        contextMenu.Items.Add(openMonitor);

        exitApplication.Text = "Exit..";
        exitApplication.Click += new EventHandler(ExitApplication_Click);
        contextMenu.Items.Add(exitApplication);
    }

所以它目前正在运作,但不是我喜欢它的工作方式。希望你能在这里帮助我,谢谢你。

2 个答案:

答案 0 :(得分:2)

将图片文件添加到项目后,单击它以显示项目属性,然后按F4。在Build Action下,将其更改为“Embedded Resource”。

您可以Stream形式访问嵌入式资源,如下所示:

public FolderMonitorApplicationContext()
{
    this.monitor =  new Monitor();

    notifyIcon = new NotifyIcon();
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
        "<project namespace>.<folder path>" + "filename.ico"))
    {
        notifyIcon.Icon = new Icon(stream);
    }

    notifyIcon.Text = "Folder Monitor";
    notifyIcon.Visible = true;

    contextMenu = new ContextMenuStrip();
    openMonitor = new ToolStripMenuItem();
    exitApplication = new ToolStripMenuItem();

    notifyIcon.ContextMenuStrip = contextMenu;

    notifyIcon.DoubleClick += NotifyIcon_DoubleClick;

    openMonitor.Text = "Open";
    openMonitor.Click += new EventHandler(OpenMonitor_Click);
    contextMenu.Items.Add(openMonitor);

    exitApplication.Text = "Exit..";
    exitApplication.Click += new EventHandler(ExitApplication_Click);
    contextMenu.Items.Add(exitApplication);
}

答案 1 :(得分:0)

使用相同的方法在表单上插入图标。 要在您的应用程序中找到要复制到系统托盘中的相同代码,请:

  1. 检查文件.resx中是否有带$符号的图标(例如:&#34; $ this.icon&#34;)
  2. 找到您的代码,在[名称] .desiner.cs。
  3. 中加入图标

    我的应用程序中的代码示例:

    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(systemtray));
    
    trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));