如何在 XAML 中获取容器(如 dll)中的特定图标? [英] How to get specific icon in a container (like dll) in XAML?

查看:23
本文介绍了如何在 XAML 中获取容器(如 dll)中的特定图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 XAML 中设置图标容器:

I can set in XAML the icon container:

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

但是如何在 XAML 中设置容器中的图标索引?类似:

But how can I set in XAML the icon index in the container ? something like:

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

或者喜欢:

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

等等...

推荐答案

事情是这样的:首先是 IValueConverter:

This is how it goes: first the 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}"/>

这篇关于如何在 XAML 中获取容器(如 dll)中的特定图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆