设置系统托盘通知图标.图标到图像文件夹中的PIC

本文关键字:图标 文件夹 PIC 图像 通知 设置 系统 | 更新日期: 2023-09-27 18:16:56

我尝试了几种方法,最终只是将图像直接放在C:'Users'Gebruiker'Documents'Visual Studio 2012'Projects'FolderMonitor'FolderMonitor'bin'Debug中。这工作现在,但理想情况下,我想设置notifyIcon。Icon = new Icon("folder.ico")到解决方案中图像文件夹中的图像。但是我不知道这是怎么回事。

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

所以它目前正在工作,但不是我想要它工作的方式。希望你能帮我解决这个问题,提前谢谢。

设置系统托盘通知图标.图标到图像文件夹中的PIC

将图片文件添加到项目中后,通过点击它来调出项目属性,并按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. 检查在文件。resx中是否有用$ symbol调用的图标(例如:"$this.icon")
  2. 找到你的代码包含图标在[name of form]. designer .cs.

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

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