1
私はカスタムボタンを持っています。 OnPaint
メソッドがコントロールのクラスファイルで動作していますが、メソッドはForm.cs
では機能しません。なぜこれが起こり、どのように修正することができますか?カスタムコントロールのpaintイベントがForm.csで動作しないのはなぜですか?
ボタンの私のコード:Form.cs
で
//code...
public AltoButton()
{
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer|ControlStyles.ResizeRedraw|ControlStyles.SupportsTransparentBackColor|ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
ForeColor = Color.Black;
Font = new System.Drawing.Font("Comic Sans MS", 10, FontStyle.Bold);
state = MouseState.Leave;
transparency = false;
}
#endregion
#region Events
protected override void OnPaint(PaintEventArgs e)
{
//code to draw shape and painting
}
//code...
コード:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
altoButton1.Paint += altoButton1_Paint;
}
void altoButton1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, 3, 3, 10, 10);
}
private void timer1_Tick(object sender, EventArgs e)
{
altoButton1.Invalidate();
}
}
を追加することです= vs.110).aspx) 'XXXX'イベントは' OnXXXX'によって生成されるので、 'OnXXXX'をオーバーライドするときに' base.OnXXXX'を呼び出してイベントを発生させるべきです。 –