0

我試圖用ControlDesigner創建一個自定義控件。我找到了一種方法來使用ControlDesinger和ClientProfile在http://code.msdn.microsoft.com/WinFormsCustomCtrl,但要使用這種方法,想要使用的項目必須手動添加控件的Design-Project dll。C#:ControlDesigner DLL作爲嵌入式資源,有可能嗎?

是否有可能在設計項目的dll嵌入資源添加到我的控制,或者是有沒有辦法設計項目的dll全自動添加到想用我的控制項目?

項目控制:

namespace NControl 
{ 
    [Designer("NDesign.MyControlDesigner, NDesign")] 
    public class MyControl : Control 
    { 
     static MyControl() 
     { 
      TypeDescriptor.AddProvider(new DynamicDesignerProvider(TypeDescriptor.GetProvider(typeof(object))), typeof(object)); 
     } 
    } 


    //########################################################################################### 
    //########################### Dynamic Design Provider from MSDN ############################## 
    //########################################################################################### 

    internal sealed class DynamicDesignerProvider : TypeDescriptionProvider 
    { 
     internal DynamicDesignerProvider(TypeDescriptionProvider parent) : base(parent) { } 

     public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) 
     { 
      if (objectType.Assembly == typeof(MyControl).Assembly) 
      { 
       IComponent component = instance as IComponent; 
       if (component != null && component.Site != null) 
       { 
        return new DesignerAttributeTypeDescriptor(base.GetTypeDescriptor(objectType, instance), component); 
       } 
      } 
      return base.GetTypeDescriptor(objectType, instance); 
     } 
    } 

    internal sealed class DesignerAttributeTypeDescriptor : CustomTypeDescriptor 
    { 
     IServiceProvider _provider; 

     internal DesignerAttributeTypeDescriptor(ICustomTypeDescriptor parent, IComponent component) 
      : base(parent) 
     { 
      if (component != null) 
      { 
       _provider = component.Site; 
      } 
     } 

     public override AttributeCollection GetAttributes() 
     { 
      AttributeCollection ac = base.GetAttributes(); 
      List<Attribute> attrs = new List<Attribute>(); 
      foreach (Attribute attr in ac) 
      { 
       DesignerAttribute dattr = attr as DesignerAttribute; 
       if (dattr != null && dattr.DesignerBaseTypeName.StartsWith("System.ComponentModel.Design.IDesigner")) 
       { 
        ITypeResolutionService trs = null; 
        if (_provider != null) 
        { 
         trs = (ITypeResolutionService)_provider.GetService(typeof(ITypeResolutionService)); 
        } 

        if (trs != null && trs.GetType(dattr.DesignerTypeName) == null) 
        { 
         DesignerAttribute da = new DesignerAttribute("System.Windows.Forms.Design.ControlDesigner, System.Design"); 
         attrs.Add(da); 
         continue; 
        } 
       } 

       attrs.Add(attr); 
      } 
      return new AttributeCollection(attrs.ToArray()); 
     } 
    } 
} 

工程設計:

namespace NDesign 
{ 
    public class MyControlDesigner : ParentControlDesigner 
    { 
     // My DesignCode 
    } 
} 

回答

0

我覺得這篇文章(http://msdn.microsoft.com/en-us/library/ee849818.aspx)將是有益的。

+0

我的控件被分成兩個dll。一個用於控制本身,一個用於ControlDesigner,用於ClientProfile。通過描述只導入MyControl.dll,而不是MyControlDesign.dll。 – Wowa 2011-02-17 09:10:13