首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

從 Microsoft.VisualStudio.Activities.dll 提取 VS2010 內置工作流活動圖標

     編輯:關於.NET

問題背景

自宿主 WF4 設計器工具箱活動無圖標,而 VS2010 官方設計器活動圖標精美。VS 安裝目錄無圖標文件,實際內嵌在程序集資源中。

目標程序集

Microsoft.VisualStudio.Activities.dll (位於 GAC 或 VS 安裝目錄)

提取核心代碼

1. 主入口:加載內置活動圖標

private static void LoadToolboxIconsForBuiltInActivities() {
    var builder = new AttributeTableBuilder();
    Assembly asm = Assembly.LoadFile(@"C:\Microsoft.VisualStudio.Activities.dll");
    var reader = new ResourceReader(asm.GetManifestResourceStream(
        "Microsoft.VisualStudio.Activities.Resources.resources"));

    foreach (Type type in asm.GetTypes()) {
        if (type.Namespace == "System.Activities.Statements")
            CreateToolboxBitmapAttributeForActivity(builder, reader, type);
    }
    MetadataStore.AddAttributeTable(builder.CreateTable());
}

2. 創建 ToolboxBitmapAttribute

private static void CreateToolboxBitmapAttributeForActivity(
    AttributeTableBuilder builder, ResourceReader reader, Type activityType) {
    
    string name = activityType.IsGenericType ? activityType.Name.Split('`')[0] : activityType.Name;
    Bitmap bmp = ExtractBitmapResource(reader, name);
    if (bmp == null) return;

    // 通過反射調用內部構造函數
    var tbaType = typeof(ToolboxBitmapAttribute);
    var imgType = typeof(Image);
    var ctor = tbaType.GetConstructor(
        BindingFlags.Instance | BindingFlags.NonPublic, null, 
        new[] { imgType, imgType }, null);
    
    var tba = (ToolboxBitmapAttribute)ctor.Invoke(new object[] { bmp, bmp });
    builder.AddCustomAttributes(activityType, tba);
}

3. 從資源讀取位圖並設置透明色

private static Bitmap ExtractBitmapResource(ResourceReader reader, string bitmapName) {
    var enum = reader.GetEnumerator();
    while (enum.MoveNext()) {
        if (String.Equals(enum.Key, bitmapName)) {
            var bmp = (Bitmap)enum.Value;
            // 右上角像素作透明色
            Color transparent = bmp.GetPixel(bmp.Width - 1, 0);
            bmp.MakeTransparent(transparent);
            return bmp;
        }
    }
    return null;
}

設計器宿主集成

// 初始化時調用
LoadToolboxIconsForBuiltInActivities();

效果對比

調用後工具箱活動圖標完美呈現,與 VS2010 一致。

遺留問題

極少數活動圖標不在該 DLL 中,仍無圖標。

環境要求

VS2010 RC 或更高版本,引用 System.Activities.Presentation、Microsoft.VisualStudio.Activities。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved