2

我尝试了几件事,最终只是将图像直接放在 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);
    }

所以它目前正在工作,但不是我希望它如何工作。希望你能在这里帮助我,在此先感谢。

4

2 回答 2

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);
}
于 2013-06-03T19:03:09.387 回答
0

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

  1. 检查文件 .resx 中是否有用 $ 符号调用的图标(例如:“$this.icon”)
  2. 找到您的代码以在 [name of form].desiner.cs 上包含图标。

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

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(systemtray));

trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
于 2014-11-17T11:33:24.323 回答