LoginSignup
17
24

More than 5 years have passed since last update.

【WPF】 .ico ファイルを Window のアイコンに設定する方法

Posted at

ググってもなかなか正解に辿りつけなかったのでメモ。

まず .ico ファイルをリソースに追加する

Resource フォルダ(なければ追加)にドラッグ・アンド・ドロップで追加します。
CropperCapture[1].png

ビルドアクションは Resource を選択してください。
CropperCapture[2].png

XAML で設定する方法

MainWindow.xaml
<Window x:Class="DiffFileSaver.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:v="clr-namespace:DiffFileSaver.Views"
        xmlns:vm="clr-namespace:DiffFileSaver.ViewModels"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
        Title="DiffFileSaver"
        Height="318.182"
        Width="525"
        Icon="/DiffFileSaver;component/Resources/unWatching.ico">

        <!-- 省略 -->

</Window>

パスの書き方は、

/<プロジェクト名>;component<リソースファイルのパス>
です。

プロパティから簡単に設定出来ます。
CropperCapture[4].png

コード側から設定する方法

MainWindow.xaml.cs
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/unWatching.ico", UriKind.RelativeOrAbsolute));

パスの書き方は XAML と異なり、
pack://application:,,,<リソースファイルのパス>
です。
こちらを用いれば動的に変更できます。

17
24
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
17
24