在以前的一篇文章:WF4.0工作流程設計工具,在WPF中宿主了一個工作流程設計工具,它的工具列如:
你會發現工具列上的活動統統沒有表徵圖,而在Visual Studio中WF設計器工具列如:
你會發現Visual Studio中的活動的表徵圖非常美觀。在你Visual Studio安裝的目錄下,是沒有這些表徵圖的。好了,現在我將從dll中將這些表徵圖找出來。
首先鎖定目標:Microsoft.VisualStudio.Activities.dll 。將它從你的C盤中找出來,拷貝到一個目錄下,我放在C:\\下面。使用下面這些代碼:
1、LoadToolboxIconsForBuiltInActivities,載入內建活動的表徵圖。
private static void LoadToolboxIconsForBuiltInActivities()
{
AttributeTableBuilder builder = new AttributeTableBuilder();
Assembly sourceAssembly = Assembly.LoadFile(@"C:\Microsoft.VisualStudio.Activities.dll");
System.Resources.ResourceReader resourceReader = new System.Resources.ResourceReader( sourceAssembly.GetManifestResourceStream("Microsoft.VisualStudio.Activities.Resources.resources"));
if (type.Namespace == "System.Activities.Statements")
{
CreateToolboxBitmapAttributeForActivity(builder, resourceReader, type); }
MetadataStore.AddAttributeTable(builder.CreateTable());
}
2、CreateToolboxBitmapAttributeForActivity
private static void CreateToolboxBitmapAttributeForActivity(AttributeTableBuilder builder, System.Resources.ResourceReader resourceReader, Type builtInActivityType)
{
System.Drawing.Bitmap bitmap = ExtractBitmapResource(resourceReader, builtInActivityType.IsGenericType ? builtInActivityType.Name.Split('`')[0] : builtInActivityType.Name);
if (bitmap != null)
{
Type tbaType = typeof(System.Drawing.ToolboxBitmapAttribute);
Type imageType = typeof(System.Drawing.Image);
ConstructorInfo constructor = tbaType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { imageType, imageType }, null);
System.Drawing.ToolboxBitmapAttribute tba = constructor.Invoke(new object[] { bitmap, bitmap }) as System.Drawing.ToolboxBitmapAttribute;
builder.AddCustomAttributes(builtInActivityType, tba);
}
}
3、ExtractBitmapResource
private static System.Drawing.Bitmap ExtractBitmapResource(System.Resources.ResourceReader resourceReader, string bitmapName)
{
System.Collections.IDictionaryEnumerator dictEnum = resourceReader.GetEnumerator();
System.Drawing.Bitmap bitmap = null;
while (dictEnum.MoveNext())
{
if (String.Equals(dictEnum.Key, bitmapName))
{
bitmap = dictEnum.Value as System.Drawing.Bitmap;
System.Drawing.Color pixel = bitmap.GetPixel(bitmap.Width-1, 0);
bitmap.MakeTransparent(pixel);
break;
}
}
return bitmap;
}
4、在工作流程器宿主中添加下面代碼:
LoadToolboxIconsForBuiltInActivities();
重新運行你的設計器,你會有驚奇發現,如:
有個別的活動圖表標不在Microsoft.VisualStudio.Activities.dll 中,所以仍然沒有顯示。
參考代碼:/Files/zhuqil/cs.rar
ps:本程式運行在Visual Studio2010 RC中