this.Controls.Add(button);
button.Font = new Font("Arial", 8);
button.Name = "btn" + idDanych;
button.Width = 100;
button.Height = 100;
button.Location = new Point(0, 0);
button.Text = "…" + Environment.NewLine + Environment.NewLine + "…";
button.ForeColor = Color.Black;
如何更改按钮第二行文字中的字体大小和颜色?
最佳答案 使用.Text属性是不可能的……
…但您可以创建一个动态位图来代替Text,允许您根据需要对其进行格式化:
Button button = new Button();
button.Name = "btn" + idDanych;
button.Width = 100;
button.Height = 100;
button.Location = new Point(0, 0);
button.Text = "";
Bitmap bmp = new Bitmap(button.ClientRectangle.Width, button.ClientRectangle.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.Clear(button.BackColor);
string line1 = "( " + Wieszak + " ) " + Haczyk;
string line2 = KodEAN;
StringFormat SF = new StringFormat();
SF.Alignment = StringAlignment.Center;
SF.LineAlignment = StringAlignment.Near;
using (Font arial = new Font("Arial", 12))
{
Rectangle RC = button.ClientRectangle;
RC.Inflate(-5, -5);
G.DrawString(line1, arial, Brushes.Black, RC, SF);
}
using (Font courier = new Font("MS Courier", 24))
{
SF.LineAlignment = StringAlignment.Center;
G.DrawString(line2, courier, Brushes.Red, button1.ClientRectangle, SF);
}
}
button.Image = bmp;
button.ImageAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(button);
您必须找出字体大小,StringFormat布局和/或定位的最佳组合,以使其看起来符合要求.还有其他DrawString()重载以不同的方式呈现文本.
但请注意,控件突出显示的方式会有所不同.在我的系统上,当鼠标进入时,标准按钮的整个区域会突出显示.使用此方法,只有边框会突出显示,因为按钮的整个中间是静态图像.