c#绘制自定义控件方法?

 我来答
dearsohucom
2012-06-26 · 超过41用户采纳过TA的回答
知道小有建树答主
回答量:127
采纳率:0%
帮助的人:113万
展开全部
重载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 ++;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式