0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WPFのアイコンについて_VSCode

Posted at

WPFのアプリを作っていてアイコンの設定が毎回よく分からなくなるのでそのメモ。
以前につくったWPFでタスクトレイに表示するアプリで設定した内容。

Visual Studioでのリソース設定とかが、VSCodeで編集する時にcsprojのどの事か分からなくなってしまうので。

環境

  • vscode .NET9

アイコン設定

logo.icoというファイルをcsprojと同じ場所に保存したとして。

タスクバーに表示

これでタスクバーとアプリ左上に表示される
image.png

  • MainWindow.xamlへアイコンを追加
<Window x:Class="TaskReminder.MainWindow"
    
    Title="MainWindow" Height="150" Width="250"
    Icon="logo.ico"></Window>
  • app.csprojへリソース追加
  <ItemGroup><Resource Include="logo.ico" />
  </ItemGroup>

タスクトレイへの表示

これでタスクトレイに表示される
image.png

  • App.xaml.csへの追加
using System.Windows;

namespace TaskReminder;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : System.Windows.Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var icon = GetResourceStream(new Uri("logo.ico", UriKind.Relative)).Stream;
        var menu = new ContextMenuStrip();
        menu.Items.Add("開く", null, Open_Click);
        menu.Items.Add("閉じる", null, Exit_Click);
        var notifyIcon = new NotifyIcon
        {
            Visible = true,
            Icon = new Icon(icon),
            Text = "リマインダー",
            ContextMenuStrip = menu
        };
        notifyIcon.MouseClick += new MouseEventHandler(Open_Click);
    }
    private void Open_Click(object? sender, EventArgs e)
    {
        MainWindow.Show();
        MainWindow.WindowState = WindowState.Normal;
    }
    private void Exit_Click(object? sender, EventArgs e)
    {
        Shutdown();
    }

exeファイルのアイコン

これでexeファイルのアイコンが変わる
と、トースト通知のアイコンにもなる

  • TaskReminder.csprojへの追加
<PropertyGroup><ApplicationIcon>logo.ico</ApplicationIcon>
</PropertyGroup>

このアイコンは一度設定すると簡単には変更できないようでした。
Visual Studio上で設定したアイコンが反映されない場合の対応方法

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?