20

在查看 System.Drawing.Font 类的构造函数时,有一个参数可以传入 System.Drawing.FontStyle 枚举中定义的 FontStyles 之一。

IE。粗斜体常规下划线

并且在实例化的对象中有粗体、斜体、下划线等布尔属性,但它们是只读的。

如果我想将我的字体定义为具有多种样式(如粗体和下划线)怎么办?

我怎样才能做到这一点?

4

3 回答 3

44

FontStyle枚举是一个Flags枚举。这意味着它的成员都是二的幂,允许您使用二进制 OR 组合它们。

例如,如果你想要粗体和下划线,你会通过

FontStyle.Bold | FontStyle.Underline

竖线 ( |) 是二元 OR 运算符。

于 2010-05-11T17:58:20.720 回答
11

在 Font 构造函数中,您可以使用 OR 运算符组合多个 FontStyle:

Font font = new Font(this.Font, FontStyle.Bold | FontStyle.Underline);
于 2010-05-11T17:59:16.830 回答
0

您可以使用类似这样的方法,以避免每种情况出现多个 if:

//define a font to use.
Font font;

font = new Font(fontname, fontsize, GraphicsUnit.Pixel);

if (bold)
    font = new Font(font, font.Style ^ FontStyle.Bold);
if (italic)
    font = new Font(font, font.Style ^ FontStyle.Italic);
if (underline)
    font = new Font(font, font.Style ^ FontStyle.Underline);
if (strikeout)
    font = new Font(font, font.Style ^ FontStyle.Strikeout);
于 2014-05-19T12:48:00.737 回答