为什么我的按钮没有显示文字?

时间:2016-11-15 10:18:52

标签: c# button text

我有一个类的CircularButton扩展了Button和一个按钮,它没有显示文本,有或没有BackColor。无法弄清楚发生了什么:

public class CircularButton : Button {


    public CircularButton(Color c, String text){
        Font = new Font("Arial", 20f, FontStyle.Bold);
        BackColor = c;
        Text = text;
    }
    public Action<PaintEventArgs> DoPaint { get; set; }
    protected override void OnPaint(PaintEventArgs e) {
      if (DoPaint != null) { DoPaint(e); }
    }
}

//make a new circular button, the text is just a letter.
CircularButton btn = new CircularButton(Color.Red,"A");
    btn.Bounds = new Rectangle(0, 0, 50, 50);
    btn.Enabled = false;                        
    btn.DoPaint = delegate(PaintEventArgs p){
        Graphics graphics = p.Graphics;
        SolidBrush brush1 = new SolidBrush(SystemColors.ButtonFace);
        graphics.FillRectangle(brush1, 0, 0, btn.Width, btn.Height);
        SolidBrush brush2 = new SolidBrush(btn.BackColor);
        graphics.FillEllipse(brush2, 0, 0, btn.Width, btn.Height);
};              

1 个答案:

答案 0 :(得分:1)

您正在覆盖OnPaint,它会在基本按钮中绘制文本。所以你必须自己写文字:

graphics.DrawString(btn.Text, btn.Font, new SolidBrush(btn.ForeColor), x, y);
相关问题