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);
}
}
}
実行時にどのようなエラーが発生しますか? – Yaman
コントロールに位置とサイズを指定しましたか?ペイントイベントでキャンバスのサイズを変更しないでください。そのイベントだけを描画します。 – LarsTech
私はそれに場所と大きさを与えてみました。私は何のエラーも出ていない、何も問題が何かを示しています。フォームはロードされますが、コントロールはロードされません。 – JW12689