C#将颜色应用于字体

Tig*_*yan 5 c# fonts drawing colors

我有这样的代码.

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#101B83");
System.Drawing.Font nameFont = new System.Drawing.Font("Tahoma", 10);
System.Drawing.Font birthdayFont = new System.Drawing.Font("Tahoma", 6);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
nameFont.Color = col;
Run Code Online (Sandbox Code Playgroud)

最后一行不起作用,因为无法找到.Color字段.为什么?

Kla*_*sen 15

因为字体没有颜色.控件可以使用字体和颜色呈现文本,但颜色不是字体的属性.

编辑:

如果您想要一个使用给定字体和颜色的文本框,您可以执行以下操作(我假设您使用的是winforms):

var myTextBox = new TextBox();
myTextBox.ForeColor = col;
myTextBox.Font = birthdayFont;
myTextBox.Text = "Happy birthday!";

this.Controls.Add(myTextBox);
Run Code Online (Sandbox Code Playgroud)