无法以编程方式添加自定义控件



我已经创建了一个自定义控件,并希望能够在运行时添加它,但它不起作用。我可以通过设计视图添加它,它工作正常。我的代码下面是自定义控件以及添加它的位置。

插件控制按钮.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)
        {
        }
    }
}

表格1.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);
        }
    }
}

好的,它现在可以工作了,我不得不为组件添加一个大小。感谢您的帮助。

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
        };

最新更新