【发布时间】:2014-06-08 23:33:15
【问题描述】:
我在 Windows 窗体中创建了一个圆形按钮。按钮很好。唯一的问题是我希望它是与背景不同的颜色,所以我将 BackColor 设置为 goldenRod。然而,它只是在圆形按钮周围创建了一个“goldenRod”立方体......我如何制作它以便只有按钮是彩色的?
public MainForm(){
InitializeComponent();
myButtonObject start = new myButtonObject();
EventHandler myHandler = new EventHandler(start_Click);
start.Click += myHandler;
start.Location = new System.Drawing.Point(5, 5);
start.Size = new System.Drawing.Size(101, 101);
start.BackColor=System.Drawing.Color.Goldenrod;
this.Controls.Add(start);
`}
void start_Click(Object sender, System.EventArgs e)
{
MessageBox.Show("Start");
}
public class myButtonObject : UserControl
{
// Draw the new button.
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Pen myPen = new Pen(Color.Black);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 100, 100);
myPen.Dispose();
}
}
【问题讨论】:
-
+1 @LarsTech,您还需要重置
BackColor否则它将填充UserControl边界作为该颜色。或者覆盖OnPaintBackground方法