teratail header banner
teratail header banner
質問するログイン新規登録

質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.30%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

3回答

11748閲覧

作成したダイアログにエラーアイコンや警告アイコンを設定、表示するには?

zanac

総合スコア25

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2016/11/14 14:02

0

0

###前提・実現したいこと
・自分で作成したダイアログにシステムアイコン(エラーアイコン、インフォーメーションアイコンなど)を
設定、表示する方法がわからず、困っています。

メッセージボックス(MessageBox)であれば、第4引数にアイコンを設定すれば良いことは分かりました。

WPFで作成したダイアログにエラーアイコンや警告アイコンを設定、表示する方法について、
ご教授お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

ベストアンサー

こんにちは。

Imaging.CreateBitmapSourceFromHIconで取得できます。
その後Imageなどで表示するようにしては如何でしょう。

使い方については以下などを参考にしてください。
http://www.kanazawa-net.ne.jp/~pmansato/wpf/wpf_graph_icon.htm

投稿2016/11/14 14:11

Tak1wa

総合スコア4791

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zanac

2016/11/14 15:47

すみません。理解力が低く、いまいちわかりませんでした・・・ 取得したいシステムアイコンを指定して取り出し、それをWPFの設定したい領域に設定する、 みたいなことは難しいのでしょうかね?
Tak1wa

2016/11/14 15:57

Imaging.CreateBitmapSourceFromHIconでリソースIDを指定すればWindowsのシステムアイコンを取り出すことが出来ます。 そして、ご自身でダイアログを作成されているとのことですのでダイアログ情にImage等で上記アイコンを表示すれば良いです。
zanac

2016/11/18 12:52

遅くなりました。 おっしゃり通りのやり方無事アイコン取得と表示ができました!! ありがとうございました!!
guest

0

WPFで作成したダイアログにエラーアイコンや警告アイコンを設定、表示する方法について、

.NET8以降でストックアイコンが使えるようになっていたので更新しときます^^
SystemIcons Class (System.Drawing) | Microsoft Learn
StockIconId Enum (System.Drawing) | Microsoft Learn

xml:MainWindow.xaml

1<Window 2 x:Class="Q55191.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Q55191" 6 Width="800" 7 Height="600" 8 ThemeMode="System"> 9 <Window.Resources> 10 <DataTemplate DataType="{x:Type local:Item}"> 11 <StackPanel> 12 <Image Width="32" Source="{Binding Source}" /> 13 <TextBlock Text="{Binding Name}" /> 14 </StackPanel> 15 </DataTemplate> 16 </Window.Resources> 17 <DockPanel> 18 <GroupBox DockPanel.Dock="Top" Header="SystemIcons"> 19 <ListBox ItemsSource="{Binding SystemIcon}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 20 <ItemsControl.ItemsPanel> 21 <ItemsPanelTemplate> 22 <WrapPanel /> 23 </ItemsPanelTemplate> 24 </ItemsControl.ItemsPanel> 25 </ListBox> 26 </GroupBox> 27 28 <GroupBox Header="StockIcons"> 29 <ListBox 30 ItemsSource="{Binding StockIcon}" 31 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 32 ScrollViewer.VerticalScrollBarVisibility="Auto"> 33 <ListBox.ItemContainerStyle> 34 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem"> 35 <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ListBoxItem_PreviewMouseLeftButtonUp" /> 36 </Style> 37 </ListBox.ItemContainerStyle> 38 <ItemsControl.ItemsPanel> 39 <ItemsPanelTemplate> 40 <WrapPanel /> 41 </ItemsPanelTemplate> 42 </ItemsControl.ItemsPanel> 43 </ListBox> 44 </GroupBox> 45 </DockPanel> 46</Window>

cs:MainWindow.xaml.cs

1using System.Drawing; 2using System.Reflection; 3using System.Windows; 4using System.Windows.Controls; 5using System.Windows.Input; 6using System.Windows.Interop; 7using System.Windows.Media; 8using System.Windows.Media.Imaging; 9 10namespace Q55191; 11 12public record Item(string Name, ImageSource Source, StockIconId StockIconId = 0); 13 14public partial class MainWindow : Window 15{ 16 public List<Item> SystemIcon { get; } 17 public List<Item> StockIcon { get; } 18 19 public MainWindow() 20 { 21 InitializeComponent(); 22 DataContext = this; 23 24 SystemIcon = typeof(SystemIcons).GetProperties(BindingFlags.Static | BindingFlags.Public) 25 .Where(x => x.PropertyType == typeof(Icon)) 26 .Select(x => 27 { 28 var i = (Icon)x.GetValue(typeof(SystemIcons), null)!; 29 var b = Imaging.CreateBitmapSourceFromHIcon(i.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 30 return new Item(x.Name, b); 31 }).ToList(); 32 33 StockIcon = Enum.GetValues<StockIconId>() 34 .Select(x => 35 { 36 using var i = SystemIcons.GetStockIcon(x); 37 var b = Imaging.CreateBitmapSourceFromHIcon(i.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 38 return new Item(x.ToString(), b, x); 39 }).ToList(); 40 } 41 42 private void ListBoxItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 43 { 44 if (sender is ListBoxItem { Content: Item item, }) 45 MyMessageBox.Show($"{item.StockIconId}", "キャプション", item.StockIconId); 46 } 47 48 private void Test() 49 { 50 // 小さすぎるのでテキトウにPadRightでサイズ合わせw 51 MessageBox.Show("MessageBox マニュフェストなし", "キャプション".PadRight(25, ' '), MessageBoxButton.YesNo, MessageBoxImage.Information); 52 53 // 「アプリケーション マニフェスト ファイル」を追加し、 54 // 「Windows のコモン コントロールとダイアログのテーマを有効にします」の部分をコメント解除 55 MessageBox.Show("MessageBox マニュフェストあり", "キャプション".PadRight(25, ' '), MessageBoxButton.YesNo, MessageBoxImage.Information); 56 57 // [NuGet Gallery | Microsoft.Windows.CsWin32](https://www.nuget.org/packages/Microsoft.Windows.CsWin32) 58 // NativeMethods.txtを作り、ShellMessageBox とだけ書く 59 //Windows.Win32.PInvoke.ShellMessageBox(null, new(new WindowInteropHelper(this).Handle), 60 // "Win32 ShellMessageBox", "キャプション", 61 // Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_YESNO | 62 // Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_ICONINFORMATION); 63 } 64}

xml:MyMessageBox.xaml

1<Window 2 x:Class="Q55191.MyMessageBox" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Background="{DynamicResource WindowBackground}" 6 ResizeMode="NoResize" 7 SizeToContent="WidthAndHeight" 8 ThemeMode="System" 9 WindowStartupLocation="CenterScreen"> 10 <WindowChrome.WindowChrome> 11 <WindowChrome CaptionHeight="{Binding ActualHeight, ElementName=stackPanel}" UseAeroCaptionButtons="False" /> 12 </WindowChrome.WindowChrome> 13 <DockPanel> 14 <StackPanel x:Name="stackPanel" DockPanel.Dock="Top"> 15 <TextBlock x:Name="caption" Margin="8,4" Text="caption" /> 16 </StackPanel> 17 <StackPanel HorizontalAlignment="Right" DockPanel.Dock="Bottom" Orientation="Horizontal"> 18 <Button MinWidth="80" Margin="8" Click="Button_Click" IsDefault="True"> 19 <!-- [Incorrect display of Button shortcut access key · Issue #10071 · dotnet/wpf](https://github.com/dotnet/wpf/issues/10071) --> 20 <AccessText Text="はい(_Y)" /> 21 </Button> 22 <Button MinWidth="80" Margin="8" IsCancel="True"> 23 <AccessText Text="いいえ(_N)" /> 24 </Button> 25 </StackPanel> 26 <StackPanel Orientation="Horizontal"> 27 <Image x:Name="icon" Width="32" Margin="8" VerticalAlignment="Top" /> 28 <TextBlock 29 x:Name="message" 30 MinWidth="300" 31 MaxWidth="500" 32 Margin="8" 33 Text="message" 34 TextWrapping="Wrap" /> 35 </StackPanel> 36 </DockPanel> 37</Window>

cs:MyMessageBox.xaml.cs

1using System.Drawing; 2using System.Windows; 3using System.Windows.Interop; 4using System.Windows.Media.Imaging; 5 6namespace Q55191; 7 8public partial class MyMessageBox : Window 9{ 10 private MyMessageBox(string messageBoxText, string caption, StockIconId iconId) 11 { 12 InitializeComponent(); 13 14 message.Text = messageBoxText; 15 this.caption.Text = caption; 16 17 using var icon = SystemIcons.GetStockIcon(iconId); 18 this.icon.Source = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 19 20 // [xaml - WPF WindowChrome unwanted black shadow - Caused by SizeToContent="WidthAndHeight" - Stack Overflow](https://stackoverflow.com/questions/57022923) 21 ContentRendered += (_, _) => InvalidateMeasure(); 22 } 23 24 public static MessageBoxResult Show(string messageBoxText, string caption, StockIconId iconId) 25 => new MyMessageBox(messageBoxText, caption, iconId).ShowDialog() == true ? MessageBoxResult.Yes 26 : MessageBoxResult.No; 27 28 private void Button_Click(object sender, RoutedEventArgs e) => DialogResult = true; 29}

NuGet Gallery | System.Drawing.Common

アプリ動画


参考までに微妙なスタイルの差異(こだわり屋さんだと結構大変そうw
各ダイアログ

投稿2024/11/18 14:53

TN8001

総合スコア10108

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

まずプロジェクトの中でリソースフォルダー(無ければ作成)にアイコン(.ico)をドラッグ&ドロップ。
そしてXAMLの中に

XAML

1 Title="Saver" 2 Height="318.182" 3 Width="525" 4 Icon="/Saver;component/Resources/Watching.ico"> 5

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

投稿2016/11/14 14:13

MasahikoHirata

総合スコア3778

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zanac

2016/11/14 15:42

ご回答ありがとうございます。WPFに設定する方法が上記記載で理解できました。 システムアイコン(エラーアイコン)の取り出し方をどのようにするものなのでしょうか?
MasahikoHirata

2016/11/14 15:47

まずファイルの検索で'*.ico'と指定して、システム内の既に登録されているアイコンを検索。これで良ければそれを使います。(上で言うドラッグ&ドロップ)また自分でも作成できますが、.icoでググればいくつかサイトがでます。例: https://jyn.jp/iconfile_how_to_create/
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問