对于模拟编译器动态创建控件的程序来说,选择图标的方式可能只是windows自带的选择图片界面,如图一,用DevExpress的朋友都知道,DevExpress的图标选择器集成了DevExpress自带的所有图标,样式也很丰富,而且过滤查找起来也很方便。如图二,若能在运行时候也能调用的话,无疑能否解决不少问题,但遗憾的是DevExpress并不提供这个调用的方法,没办法只能从源代码里面抠出来(其实就是比较懒去画界面),并且做了适当修改封装成dll。演示如图三 和图四,相比dev的,这里我加了一个清除按钮和导入功能,最大的亮点是增加了一个更友好的功能——自定义图标载入,可以利用ImageCollection载入自己的图标集,当然还有一种是运行时候的导入,只要资源没释放,则导入的图标集一直存在,操作方式demo已经提供。
demo及源代码的下载路径:http://download.youkuaiyun.com/detail/u012097590/9663888
Demo 下载路径:http://pan.baidu.com/s/1eSibHbs
图一
图二
图三 图标选择器演示——ImageCollection载入
图四 文件夹载入演示
若想自己写一个的话,下面的代码可以做为参考(代码未优化),教大家怎么读取devexpress 封装好的图标。
public static class DXImageGalleryLoader
{
public static DevExpress.XtraBars.Ribbon.GalleryItemCollection Load()
{
DevExpress.XtraBars.Ribbon.GalleryItemCollection dataModel = new DevExpress.XtraBars.Ribbon.GalleryItemCollection();
using (System.Resources.ResourceReader reader = GetResourceReader(DevExpress.Utils.DxImageAssemblyUtil.ImageAssembly))
{
System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
string key = (string)dict.Key as string;
if (!DevExpress.Utils.DxImageAssemblyUtil.ImageProvider.IsBrowsable(key)) continue;
if (IsImageBasedResource(key))
{
Image image = GetImageFromStream((System.IO.Stream)dict.Value);
if (image != null)
dataModel.Add(new DevExpress.XtraBars.Ribbon.GalleryItem(image, image.ToString(), key));
}
}
}
return dataModel;
}
static bool IsImageBasedResource(string key)
{
return key.EndsWith(".png", StringComparison.Ordinal);
}
static System.Resources.ResourceReader GetResourceReader(System.Reflection.Assembly imagesAssembly)
{
var resources = imagesAssembly.GetManifestResourceNames();
var imageResources = Array.FindAll(resources, resourceName => resourceName.EndsWith(".resources"));
if (imageResources.Length != 1)
{
throw new Exception("读取异常");
}
return new System.Resources.ResourceReader(imagesAssembly.GetManifestResourceStream(imageResources[0]));
}
static Image GetImageFromStream(System.IO.Stream stream)
{
Image res = null;
try
{
res = Image.FromStream(stream);
}
catch { res = null; }
return res;
}
/// <summary>
/// 初始化galleryControl
/// </summary>
void InitGallery()
{
galleryControl1.Gallery.Groups.Clear();
GalleryItemGroup group = new GalleryItemGroup() { Caption = "Custom" };
galleryControl1.Gallery.Groups.Add(group);
group.Items.AddRange(DXImageGalleryLoader.Load().Take(1000).ToArray());
}
}