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