加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

wpf – 如何在XAML中的容器(如dll)中获取特定图标?

发布时间:2020-12-13 22:30:01 所属栏目:Windows 来源:网络整理
导读:我可以在XAML中设置图标容器: Image Source="Shell32.dll.ico" / 但是如何在XAML中设置容器中的图标索引?就像是: Image Source="Shell32.dll,5" / 或者像: Image Source="Shell32.dll" Index="5" / 等等… 解决方法 这就是它的方式:首先是IValueConvert
我可以在XAML中设置图标容器:

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

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

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

或者像:

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

等等…

解决方法

这就是它的方式:首先是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,CultureInfo culture)
    { throw new NotImplementedException(); }
}

XAML:

<Image Source="{Binding Converter={StaticResource iconExtractor},ConverterParameter=c:WindowsSystem32shell32.dll|72}"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读