简体   繁体   中英

C# set icon of shortcut when setting icon of application

I'm trying to set the icon of my application (visible in the taskbar). The icon is correct when I run the .exe itself or run it from visual studio, but this doesn't apply when starting from a shortcut.. The icon inside the application itself, top left corner, is correct.

Code used to set the icon:

var assemblyDirectory = Directory.GetCurrentDirectory();
var iconUri = new Uri(Path.Combine(assemblyDirectory, resourceName), UriKind.Absolute);
Icon= BitmapFrame.Create(iconUri);

I'm trying to add a red circle indicating changes in my application to the users, which is done by changing between 2 icons.

Any idea on how to set the icon of a shortcut at runtime, or about how to show a red circle in the taskbar-icon indicating changes?

Although it is impossible to set the assembly icon during runtime (it is read-only), you can use NotifyIcon to display an icon in system tray and that is fairly easy to change during runtime.

Here is an example of how to implement a NotifyIcon: https://www.codeproject.com/Tips/627796/Doing-a-NotifyIcon-program-the-right-way

Note: Since you are using WPF, you need to include a reference to Windows.Forms in your application.

Edit: just in case the link goes dead, here some code example for Program.cs, needs an "icon" imported as resource:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new MyCustomApplicationContext());
}

public class MyCustomApplicationContext : ApplicationContext
{
    public static NotifyIcon trayIcon;

    public MyCustomApplicationContext()
    {
        trayIcon = new NotifyIcon()
        {
            Icon = icon,
            ContextMenu = new ContextMenu(new MenuItem[]
            {
                new MenuItem("Exit", Exit),
                new MenuItem("Do something", DoSomething),
            }),
            Visible = true,
            BalloonTipText = "My Application",
            BalloonTipTitle = "My Application",
            Text = "My Application Text"
        };
        trayIcon.Click += new System.EventHandler(trayIcon_Click);
    }

    private void trayIcon_Click(object sender, System.EventArgs e)
    {
        //Do something
    }

    void DoSomething(object sender, EventArgs e)
    {
    }

    void Exit(object sender, EventArgs e)
    {
        // Hide tray icon, otherwise it will remain shown until user mouses over it
        trayIcon.Visible = false;

        Application.Exit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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