您不能使用 BackgroundColor 属性同时拥有具有两种颜色的按钮。
首先,您必须创建一个组件按钮并自定义。
右键单击>添加新项目>从Windows窗体中选择CustomControl>输入名称>确定
之后,编辑代码就足够了。该类继承自按钮类而不是控件类
public partial class TwoColorButton : Button
{
Color clr1, clr2;
private Color color1 = Color.DodgerBlue;
private Color color2 = Color.MidnightBlue;
private int angle = 90;
private int textX = 100;
private int textY = 25;
private String text = "";
public int ButtonAngle
{
get { return angle; }
set { angle = value; Invalidate(); }
}
public String ButtonText
{
get { return text; }
set { text = value; Invalidate(); }
}
public Color StartColor
{
get { return color1; }
set { color1 = value; Invalidate(); }
}
public Color EndColor
{
get { return color2; }
set { color2 = value; Invalidate(); }
}
public int GradientAngle
{
get { return angle; }
set { angle = value; Invalidate(); }
}
public int TextLocation_X
{
get { return textX; }
set { textX = value; Invalidate(); }
}
public int TextLocation_Y
{
get { return textY; }
set { textY = value; Invalidate(); }
}
public TwoColorButton()
{
this.Size = new Size(100, 40);
this.BackColor = Color.Transparent;
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.FlatAppearance.MouseOverBackColor = Color.Transparent;
this.FlatAppearance.MouseDownBackColor = Color.Transparent;
text = this.Text;
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
color1 = clr1;
color2 = clr2;
this.Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
textX = (int)((this.Width / 3) - 1);
textY = (int)((this.Height / 3) + 5);
}
//draw circular button function
//draw rectangular button function
void DrawRectangularButton(Graphics g)
{
Color c1 = Color.FromArgb(250, color1);
Color c2 = Color.FromArgb(250, color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
g.FillRectangle(b, 0, 0, this.Width, this.Height);
for (int i = 0; i < 2; i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), this.Width - i, 0, this.Width - i, this.Height);
g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0, this.Height - i, this.Width, this.Height - i);
g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0 + i, 0, 0 + i, this.Height);
g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0, 0 + i, this.Width, i);
}
Point p = new Point(textX, textY);
SolidBrush frcolor = new SolidBrush(this.ForeColor);
g.DrawString(text, this.Font, frcolor, p);
b.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.DrawRectangularButton(e.Graphics);
}
}
More read about this