gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:15:09 25 4
gpt4 key购买 nike

我可以在 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, Type targetType, object parameter, CultureInfo culture)
{ throw new NotImplementedException(); }
}

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

关于wpf - 如何在 XAML 中获取容器(如 dll)中的特定图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com