How to set multiple FontStyles when instantiating a font?

♀尐吖头ヾ 提交于 2019-12-01 03:00:18

The FontStyle enum is a Flags enum. This means that its members are all powers of two, allowing you to combine them using a binary OR.

For example, if you want bold and underline, you'd pass

FontStyle.Bold | FontStyle.Underline

The vertical bar (|) is the binary OR operator.

In the Font constructor, you can combine multiple FontStyles using the OR operator:

Font font = new Font(this.Font, FontStyle.Bold | FontStyle.Underline);

You could use something like this, in order to avoid multiple ifs for each case:

//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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!