C# 将颜色应用于字体

发布于 2024-09-28 16:47:53 字数 420 浏览 1 评论 0原文

我有这样的代码。

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;

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

I have code like this.

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;

Last line doesn't work, because .Color field cannot be found. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮子 2024-10-05 16:47:53

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

编辑:

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

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

this.Controls.Add(myTextBox);

Because a font does not have a color. A control can render text using a font and a color, but the color is not a property of the font.

EDIT:

If you want a textbox that uses a given font and color you can do the following (I'm assuming that you are using winforms):

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

this.Controls.Add(myTextBox);
幻想少年梦 2024-10-05 16:47:53

字体没有颜色。您可以在绘图代码本身中使用颜色,或者通过 Control.ForeColor 属性使用颜色

Fonts do not have colors. You use colors in the drawing code itself, or with the Control.ForeColor property

顾北清歌寒 2024-10-05 16:47:53

将颜色设置为控件的 ForeColor 属性。这将设置您想要的字体颜色。
您不能直接设置字体颜色。
您必须单独设置字体和前景色以进行控制。

set color to control's ForeColor property. this will set the desired color of your font.
You cannot directly set color to font.
you will have to set font and forecolor separately for control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文