2011-07-02 10 views
6

私は、XAMLのアイコンコンテナ設定することができます。XAMLのコンテナ(dllなど)に特定のアイコンを表示するにはどうすればよいですか?

<Image Source="Shell32.dll.ico" /> 

しかし、どのように、私はコンテナにXAMLのアイコンのインデックスを設定することができますか?

<Image Source="Shell32.dll,5" /> 

など:何か

<Image Source="Shell32.dll" Index="5" /> 

等...

+2

あなたの最初のバージョンは私にとってはうまくいかないようです。 – svick

+2

「Shell32.dll.ico」という名前のファイルがない可能性があります。これは単なるデモンストレーションでした。 – Tar

+0

私はShell32.dllを自分のプロジェクトにコピーして、Shell32.dll.icoに名前を変更しようとしました。 – svick

答えて

4

これは、それがどのようになるです:最初IValueConverter

using System; 
using System.Diagnostics; 
using System.Globalization; 
using System.Runtime.InteropServices; 
using System.Windows; 
using System.Windows.Data; 
using System.Windows.Interop; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

[ValueConversion(typeof(string), typeof(ImageSource))] 
public class HabeasIcon : IValueConverter 
{ 
    [DllImport("shell32.dll")] 
    private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex); 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string[] fileName = ((string)parameter).Split('|'); 

     if (targetType != typeof(ImageSource)) 
      return Binding.DoNothing; 

     IntPtr hIcon = ExtractIcon(Process.GetCurrentProcess().Handle, fileName[0], int.Parse(fileName[1])); 

     ImageSource ret = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
     return ret; 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { throw new NotImplementedException(); } 
} 

XAML:

<Image Source="{Binding Converter={StaticResource iconExtractor}, ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/> 
関連する問題