【问题标题】:C# apply Color to FontC# 将颜色应用于字体
【发布时间】:2011-04-27 08:39:33
【问题描述】:

我有这样的代码。

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 字段。为什么?

【问题讨论】:

  • 你听说过namespaces吗?

标签: c# fonts colors drawing


【解决方案1】:

将颜色设置为控件的 ForeColor 属性。这将设置所需的字体颜色。 您不能直接将颜色设置为字体。 您必须分别设置字体和前景色以进行控制。

【讨论】:

    【解决方案2】:

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

    编辑:

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

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

    【讨论】:

    • 我在教程中看到了使用颜色的例子......好的......如何使用 Control 来做到这一点?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2013-04-02
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多