2011-07-02 5 views
6

puedo fijar en XAML el icono de contenedor:¿Cómo obtener un ícono específico en un contenedor (como dll) en XAML?

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

Pero, ¿cómo puedo configurar en XAML el índice de icono en el contenedor? algo así como:

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

O como:

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

etc ...

+2

Su primera versión no parece funcionar para mí. – svick

+2

Porque probablemente no tenga un archivo llamado "Shell32.dll.ico" ... eso fue solo una demostración. – Tar

+0

Intenté copiar Shell32.dll a mi proyecto y cambiarle el nombre a Shell32.dll.ico. – svick

Respuesta

4

Esto es lo que pasa: la primera 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(); } 
} 

El XAML:

<Image Source="{Binding Converter={StaticResource iconExtractor}, ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/> 
Cuestiones relacionadas