C# 将颜色应用于字体
我有这样的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为字体没有颜色。控件可以使用字体和颜色呈现文本,但颜色不是字体的属性。
编辑:
如果您想要一个使用给定字体和颜色的文本框,您可以执行以下操作(我假设您正在使用 winforms):
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):
字体没有颜色。您可以在绘图代码本身中使用颜色,或者通过
Control.ForeColor
属性使用颜色Fonts do not have colors. You use colors in the drawing code itself, or with the
Control.ForeColor
property将颜色设置为控件的 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.