c#绘制自定义控件方法?
1个回答
展开全部
重载OnPaint就可以了啊
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
RectangleF m_OutPaintRectangle = new RectangleF(this.ClientRectangle.Location, this.ClientRectangle.Size);
RectangleF m_InPaintRectangle = new RectangleF();
RectangleF m_CenterRectangle = new RectangleF();
RectangleF m_CircleRectangle = new RectangleF();
StringFormat m_TextFormat = new StringFormat();
int m_iCircleLoop;
m_TextFormat.Alignment = StringAlignment.Center;
if (m_OutPaintRectangle.Width > m_OutPaintRectangle.Height)
{
m_OutPaintRectangle.Width = m_OutPaintRectangle.Height;
}
else
{
m_OutPaintRectangle.Height = m_OutPaintRectangle.Width;
}
m_InPaintRectangle = m_OutPaintRectangle;
m_CenterRectangle = m_OutPaintRectangle;
m_OutPaintRectangle.Inflate(-20, -20);
e.Graphics.DrawEllipse(Pens.Black, m_OutPaintRectangle);
m_InPaintRectangle.Inflate(-50, -50);
e.Graphics.DrawEllipse(Pens.Black, m_InPaintRectangle);
m_CenterRectangle.Inflate(-m_CenterRectangle.Width / 10 * 9 / 2, -m_CenterRectangle.Height / 10 * 9 / 2);
e.Graphics.DrawEllipse(Pens.Black, m_CenterRectangle);
e.Graphics.DrawString(m_iRoundCount.ToString(), new Font("Times New Roman", 15), Brushes.Red, new PointF( m_CenterRectangle.X + m_CenterRectangle.Width / 2 , m_CenterRectangle.Y + m_CenterRectangle.Height / 2 - 8 ), m_TextFormat);
for (m_iCircleLoop = 0; m_iCircleLoop < m_iCupCount; m_iCircleLoop++)
{
m_CircleRectangle.X = m_CenterRectangle.Left + m_CenterRectangle.Width / 2 + (float)((m_InPaintRectangle.Width / 2 - (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Cos(2 * Math.PI / m_iCupCount * m_iCircleLoop));
m_CircleRectangle.Y = m_CenterRectangle.Top + m_CenterRectangle.Height / 2 + (float)((m_InPaintRectangle.Width / 2 - (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Sin(2 * Math.PI / m_iCupCount * m_iCircleLoop));
RectangleF m_TextRectangle = new RectangleF(m_CircleRectangle.X - m_fTextR, m_CircleRectangle.Y - m_fTextR, m_fTextRScale * m_fTextR, m_fTextRScale * m_fTextR);
if (m_iCircleLoop == 0)
{
e.Graphics.DrawLine(Pens.Red, new PointF(m_CenterRectangle.Left + m_CenterRectangle.Width / 2, m_CenterRectangle.Top + m_CenterRectangle.Height / 2), new PointF(m_OutPaintRectangle.X + m_OutPaintRectangle.Width, m_OutPaintRectangle.Y + m_OutPaintRectangle.Height / 2));
}
if( e.ClipRectangle.Contains( new Point( ( int )m_CircleRectangle.X, ( int )m_CircleRectangle.Y ) ) )
{
e.Graphics.DrawString(( m_iCupText[m_iCircleLoop] + 1 ).ToString(), new Font("Times New Roman", 7), Brushes.Black, m_TextRectangle, m_TextFormat);
}
m_CircleRectangle.X = m_CenterRectangle.Left + m_CenterRectangle.Width / 2 + (float)((m_InPaintRectangle.Width / 2 + (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Cos(2 * Math.PI / m_iCupCount * m_iCircleLoop));
m_CircleRectangle.Y = m_CenterRectangle.Top + m_CenterRectangle.Height / 2 + (float)((m_InPaintRectangle.Width / 2 + (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Sin(2 * Math.PI / m_iCupCount * m_iCircleLoop));
RectangleF m_CupRectangle = new RectangleF(m_CircleRectangle.X - m_fCupCircleR, m_CircleRectangle.Y - m_fCupCircleR, 2 * m_fCupCircleR, 2 * m_fCupCircleR);
e.Graphics.DrawEllipse(Pens.Purple, m_CupRectangle);
m_CupRectangle.Inflate(-2, -2);
//m_CupBrush[0] = Brushes.Tomato;
e.Graphics.FillEllipse(m_CupBrush[m_iCircleLoop], m_CupRectangle);
}
m_iTest ++;
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
RectangleF m_OutPaintRectangle = new RectangleF(this.ClientRectangle.Location, this.ClientRectangle.Size);
RectangleF m_InPaintRectangle = new RectangleF();
RectangleF m_CenterRectangle = new RectangleF();
RectangleF m_CircleRectangle = new RectangleF();
StringFormat m_TextFormat = new StringFormat();
int m_iCircleLoop;
m_TextFormat.Alignment = StringAlignment.Center;
if (m_OutPaintRectangle.Width > m_OutPaintRectangle.Height)
{
m_OutPaintRectangle.Width = m_OutPaintRectangle.Height;
}
else
{
m_OutPaintRectangle.Height = m_OutPaintRectangle.Width;
}
m_InPaintRectangle = m_OutPaintRectangle;
m_CenterRectangle = m_OutPaintRectangle;
m_OutPaintRectangle.Inflate(-20, -20);
e.Graphics.DrawEllipse(Pens.Black, m_OutPaintRectangle);
m_InPaintRectangle.Inflate(-50, -50);
e.Graphics.DrawEllipse(Pens.Black, m_InPaintRectangle);
m_CenterRectangle.Inflate(-m_CenterRectangle.Width / 10 * 9 / 2, -m_CenterRectangle.Height / 10 * 9 / 2);
e.Graphics.DrawEllipse(Pens.Black, m_CenterRectangle);
e.Graphics.DrawString(m_iRoundCount.ToString(), new Font("Times New Roman", 15), Brushes.Red, new PointF( m_CenterRectangle.X + m_CenterRectangle.Width / 2 , m_CenterRectangle.Y + m_CenterRectangle.Height / 2 - 8 ), m_TextFormat);
for (m_iCircleLoop = 0; m_iCircleLoop < m_iCupCount; m_iCircleLoop++)
{
m_CircleRectangle.X = m_CenterRectangle.Left + m_CenterRectangle.Width / 2 + (float)((m_InPaintRectangle.Width / 2 - (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Cos(2 * Math.PI / m_iCupCount * m_iCircleLoop));
m_CircleRectangle.Y = m_CenterRectangle.Top + m_CenterRectangle.Height / 2 + (float)((m_InPaintRectangle.Width / 2 - (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Sin(2 * Math.PI / m_iCupCount * m_iCircleLoop));
RectangleF m_TextRectangle = new RectangleF(m_CircleRectangle.X - m_fTextR, m_CircleRectangle.Y - m_fTextR, m_fTextRScale * m_fTextR, m_fTextRScale * m_fTextR);
if (m_iCircleLoop == 0)
{
e.Graphics.DrawLine(Pens.Red, new PointF(m_CenterRectangle.Left + m_CenterRectangle.Width / 2, m_CenterRectangle.Top + m_CenterRectangle.Height / 2), new PointF(m_OutPaintRectangle.X + m_OutPaintRectangle.Width, m_OutPaintRectangle.Y + m_OutPaintRectangle.Height / 2));
}
if( e.ClipRectangle.Contains( new Point( ( int )m_CircleRectangle.X, ( int )m_CircleRectangle.Y ) ) )
{
e.Graphics.DrawString(( m_iCupText[m_iCircleLoop] + 1 ).ToString(), new Font("Times New Roman", 7), Brushes.Black, m_TextRectangle, m_TextFormat);
}
m_CircleRectangle.X = m_CenterRectangle.Left + m_CenterRectangle.Width / 2 + (float)((m_InPaintRectangle.Width / 2 + (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Cos(2 * Math.PI / m_iCupCount * m_iCircleLoop));
m_CircleRectangle.Y = m_CenterRectangle.Top + m_CenterRectangle.Height / 2 + (float)((m_InPaintRectangle.Width / 2 + (m_OutPaintRectangle.Width - m_InPaintRectangle.Width) / 4) * System.Math.Sin(2 * Math.PI / m_iCupCount * m_iCircleLoop));
RectangleF m_CupRectangle = new RectangleF(m_CircleRectangle.X - m_fCupCircleR, m_CircleRectangle.Y - m_fCupCircleR, 2 * m_fCupCircleR, 2 * m_fCupCircleR);
e.Graphics.DrawEllipse(Pens.Purple, m_CupRectangle);
m_CupRectangle.Inflate(-2, -2);
//m_CupBrush[0] = Brushes.Tomato;
e.Graphics.FillEllipse(m_CupBrush[m_iCircleLoop], m_CupRectangle);
}
m_iTest ++;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询