2017-01-19 52 views
0

我已經創建了自定義控件並希望能夠在運行時添加它,但它不起作用。我可以通過設計視圖添加它,它工作正常。我的代碼是自定義控件的下圖,以及我添加的位置。無法通過編程添加自定義控件

PluginControlButton.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace customControlLib 
{ 
    public partial class PluginControlButton : Control 
    { 
     private Color color1 = Color.LightGray; 
     private Color color2 = Color.Black; 
     private Color color3 = Color.White; 
     private Color colorU = Color.Yellow; 
     private int cpSize = 70; 
     private Image backgroundImage; 
     /// <summary> 
     /// The image for the plugin 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public Image PluginImage 
     { 
      get { return backgroundImage; } 
      set { backgroundImage = value; Invalidate(); } 
     } 
     /// <summary> 
     /// The size of the component 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public int ComponentSize 
     { 
      get { return cpSize; } 
      set 
      { 
       if (value == cpSize) 
        return; 
       cpSize = value; 
       cpSize = value; 

       Invalidate(); 
      } 
     } 
     /// <summary> 
     /// The color of the out ring 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public Color ProcessColor 
     { 
      get { return color1; } 
      set { color1 = value; Invalidate(); } 
     } 
     /// <summary> 
     /// The color of the inside circle 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public Color InnerColor 
     { 
      get { return color2; } 
      set { color2 = value; Invalidate(); } 
     } 
     /// <summary> 
     /// The color of the update circle 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public Color UpdateColor 
     { 
      get { return colorU; } 
      set { colorU = value; Invalidate(); } 
     } 
     /// <summary> 
     /// The color of the text 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public Color textColor 
     { 
      get { return color3; } 
      set { color3 = value; Invalidate(); } 
     } 
     /// <summary> 
     /// Plugin text 
     /// </summary> 
     [Category("PluginControlSettings")] 
     public new string Text 
     { 
      get { return base.Text; } 
      set 
      { 
       if (value == base.Text) 
        return; 
       base.Text = value; 

       Invalidate(); 
      } 
     } 
     public PluginControlButton() 
     { 
      InitializeComponent(); 
     } 
     protected override void OnPaint(PaintEventArgs pe) 
     { 
      this.Width = cpSize; 
      this.Height = cpSize; 
      Graphics gfx = pe.Graphics; 
      Rectangle rc = ClientRectangle; 
      rc.Width -= 1; 
      rc.Height -= 1; 
      Graphics gfx2 = pe.Graphics; 
      Rectangle rc2 = ClientRectangle; 
      rc2.Width -= 17; 
      rc2.Height -= 17; 
      rc2.Location = new Point(8, 8); 
      Graphics gfxU = pe.Graphics; 
      Rectangle rcU = ClientRectangle; 
      rcU.Width -= 9; 
      rcU.Height -= 9; 
      rcU.Location = new Point(4, 4); 
      gfx.FillRectangle(new SolidBrush(Parent.BackColor), ClientRectangle); 
      gfx.FillEllipse(new SolidBrush(color1), rc); 
      gfx.DrawEllipse(new Pen(Color.Transparent, 1.0f), rc); 
      gfxU.FillEllipse(new SolidBrush(colorU), rcU); 
      gfxU.DrawEllipse(new Pen(Color.Transparent, 1.0f), rcU); 
      gfx2.FillEllipse(new SolidBrush(color2), rc2); 
      gfx2.DrawEllipse(new Pen(Color.Transparent, 1.0f), rc2); 
      Font fnt = new Font(base.Font.FontFamily, base.Font.Size, base.Font.Style, GraphicsUnit.Pixel); 
      StringFormat sf = new StringFormat(); 
      sf.Alignment = StringAlignment.Center; 
      sf.LineAlignment = StringAlignment.Center; 
      gfx.DrawString(Text, fnt, new SolidBrush(color3), new RectangleF((float)(rc2.Left), (float)(rc2.Top), (float)(rc2.Width), (float)(rc2.Height)), sf); 
      if (backgroundImage != null) 
      { 
       Graphics gfx3 = pe.Graphics; 
       Rectangle rc3 = ClientRectangle; 
       rc3.Width -= 35; 
       rc3.Height -= 35; 
       rc3.Location = new Point(17, 17); 
       gfx3.DrawImage(backgroundImage, rc3); 
       base.ForeColor = color2; 
      } 
     } 
     protected override void OnPaintBackground(PaintEventArgs pevent) 
     { 
     } 
    } 
} 

Form1.cs的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using customControlLib; 

namespace GlassSquid2 
{ 
    public partial class GlassSquidControl : Form 
    { 
     public GlassSquidControl() 
     { 
      InitializeComponent(); 

     } 

     private void GlassSquidControl_Load(object sender, EventArgs e) 
     { 
      PluginControlButton pbc = new PluginControlButton(); 

      this.Controls.Add(pbc); 
     } 
    } 
} 
+0

在運行時會出現什麼錯誤? – Yaman

+0

你是否給了控件一個位置和大小?永遠不要在繪畫事件中調整畫布的大小 - 只能繪製該事件,沒有別的。 – LarsTech

+0

我試着給它一個位置和大小。我沒有收到任何錯誤,沒有顯示出問題所在。表單加載但控件不會加載。 – JW12689

回答

0

好了,現在的工作,我只好一個尺寸添加到組件。謝謝您的幫助。

customControlLib.PluginControlButton pbc = new customControlLib.PluginControlButton() 
     { 
      ComponentSize = 70, 
      InnerColor = Color.Black, 
      Name = "TEST", 
      PluginImage = null, 
      ProcessColor = System.Drawing.Color.LightGray, 
      Size = new System.Drawing.Size(70, 70), 
      Text = "Test", 
      textColor = System.Drawing.Color.White, 
      UpdateColor = System.Drawing.Color.Yellow 
     };