动态创建的圆形按钮bakgroundcolor没有变化 [英] Dynamically created roundbutton bakgroundcolor is not changing
问题描述
大家好,
任何人都可以告诉我为什么动态创建的圆形按钮在启动时不会改变背景颜色的原因。样本代码如下所示:
使用 System.Windows.Forms;
使用 System.Drawing;
命名空间 MyNamespace
{
public class RoundButton:UserControl
{
public Color backgroundColor = Color.LimeGreen;
受保护 覆盖 void OnPaint (PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int penWidth = 4 ;
Pen pen = new Pen(Color.Black, 4 );
int fontHeight = 10 ;
Font font = new 字体( Arial ,fontHeight);
SolidBrush brush = new SolidBrush(backgroundColor);
graphics.FillEllipse(brush, 0 , 0 ,宽度,高度);
SolidBrush textBrush = new SolidBrush(Color.Black);
graphics.DrawEllipse(pen,( int )penWidth / 2 ,
( int )penWidth / 2 ,宽度 - penWidth,Height - penWidth);
graphics.DrawString(Text,font,textBrush,penWidth,
Height / 2 - fontHeight);
}
}
}
// 用法:
public class MyForm:Form
{
public MyForm()
{
RoundButton roundButton = new RoundButton的();
EventHandler handler = new EventHandler(roundButton_Click);
// roundButton.Click + = handler;
roundButton.Text = 点击这里!;
roundButton.backgroundColor = System.Drawing.Color.LimeGreen;
roundButton.Size = new System.Drawing.Size( 80 , 80 跨度>);
roundButton.Location = new System.Drawing.Point( 100 , 30 跨度>);
this .Controls.Add(roundButton);
}
}
public void button1_click( object sender,EventArgs e)
{
timer1.Start();
}
private void timer1_Tick( object sender,EventArgs e)
{
if (roundButton [ 1 ].backgroundColor == System.Drawing.Color.LimeGreen)
{
roundButton [ 1 ]。backgroundColor = System.Drawing。红色;
}
else if (roundButton [ 1 ]。backgroundColor == System.Drawing.Color.Red)
{
roundButton [ 1 ]。backgroundColor = System.Drawing.Color.LimeGreen;
}
}
提前谢谢...
< blockquote>你需要刷新控件,因为它不知道需要重新绘制。
如果( roundButton [ 1 ]。backgroundColor == System.Drawing.Color.LimeGreen)
{
roundButton [ 1 ]。backgroundColor = System.Drawing.Color.Red;
roundButton [ 1 ]。Refresh(); // 应该这样做
}
当您更改标准控件的BackColor时,系统会为您处理所有这些内容(无效和重新绘制)。但是你已经定义了一个包含backcolor的单独属性 - 如果你改变它,必须通知控件。
如果你使用默认的BackColor属性,它应该工作无需手动调用刷新。
Hi all,
can anybody tell me the reason why the dynamically created roundbutton doesn''t change its background color when initiated to do so.. The sample code is showwn below:
using System.Windows.Forms;
using System.Drawing;
namespace MyNamespace
{
public class RoundButton : UserControl
{
public Color backgroundColor = Color.LimeGreen;
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int penWidth = 4;
Pen pen = new Pen(Color.Black, 4);
int fontHeight = 10;
Font font = new Font("Arial", fontHeight);
SolidBrush brush = new SolidBrush(backgroundColor);
graphics.FillEllipse(brush, 0, 0, Width, Height);
SolidBrush textBrush = new SolidBrush(Color.Black);
graphics.DrawEllipse(pen, (int)penWidth / 2,
(int)penWidth / 2, Width - penWidth, Height - penWidth);
graphics.DrawString(Text, font, textBrush, penWidth,
Height / 2 - fontHeight);
}
}
}
//Usage:
public class MyForm : Form
{
public MyForm()
{
RoundButton roundButton = new RoundButton();
EventHandler handler = new EventHandler(roundButton_Click);
//roundButton.Click += handler;
roundButton.Text = "Click Here!";
roundButton.backgroundColor = System.Drawing.Color.LimeGreen;
roundButton.Size = new System.Drawing.Size(80, 80);
roundButton.Location = new System.Drawing.Point(100, 30);
this.Controls.Add(roundButton);
}
}
public void button1_click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (roundButton[1].backgroundColor == System.Drawing.Color.LimeGreen)
{
roundButton[1].backgroundColor = System.Drawing.Color.Red;
}
else if(roundButton[1].backgroundColor == System.Drawing.Color.Red)
{
roundButton[1].backgroundColor = System.Drawing.Color.LimeGreen;
}
}
Thanks in advance...
You need to Refresh the control because it does not know that repainting is necessary.
if (roundButton[1].backgroundColor == System.Drawing.Color.LimeGreen) { roundButton[1].backgroundColor = System.Drawing.Color.Red; roundButton[1].Refresh(); // that should do the trick }
When you change the BackColor of a standard control, the system takes care of all that stuff for you (invalidating and repainting). But you have defined a separate property with the containing backcolor - if you change it, the control must be informed.
If you use the default BackColor propery, it should work without calling Refresh manually.
这篇关于动态创建的圆形按钮bakgroundcolor没有变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!