简体   繁体   English

如何从WPF中的C#代码访问应用程序图标(从VS设置)

[英]How to access to Application Icon (set from VS) from C# code in WPF

Before asking the question, I looked for a possible similar or duplicate question, but I think I did not find it. 在提出问题之前,我寻找了可能类似或重复的问题,但我认为没有找到。

I will try to be synthetic and precise. 我将尝试综合而精确。 I have correctly set the application icon through Visual Studio (Project > Settings > Application > Resources > Icon and Manifest > Icon). 我已经通过Visual Studio正确设置了应用程序图标(项目>设置>应用程序>资源>图标和清单>图标)。 I would like to know how to access that icon from the C# code. 我想知道如何从C#代码访问该图标。 How can I do it? 我该怎么做?


Going specifically, I would like to set that icon for the NotifyIcon object. 具体来说,我想为NotifyIcon对象设置该图标。 Now I do not know if there is a standard procedure or not, but I would like to avoid loading two identical icons, one for the application and the other for the NotifyIcon object. 现在,我不知道是否存在标准过程,但我想避免加载两个相同的图标,一个用于应用程序,另一个用于NotifyIcon对象。 I tried using SystemIcons.Application but it does not work. 我尝试使用SystemIcons.Application但它不起作用。


The screenshot: 屏幕截图:

在此处输入图片说明

The code: 编码:

class MyHandsomeClass
{
        private NotifyIcon ni = new NotifyIcon();

        // ...

        private void NotifyError()
        {
            ni.Text = "Example";
            ni.BalloonTipText = "Un problema sconosciuto impedisce l'avvio del gioco, si prega di contattare gli amministratori";
            ni.BalloonTipTitle = "Impossibile eseguire il gioco";
            ni.Icon = SystemIcons.Application;     // This is not working
            ni.BalloonTipIcon = ToolTipIcon.Error; // This is ok
            ni.Visible = true;
            ni.ShowBalloonTip(5000);
        }

        // ...

}

Try the following: 请尝试以下操作:

ni.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);

This finds the EXE and takes the icon from there 这将找到EXE并从此处获取图标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM