vb.net中的多种字体样式组合 [英] Multiple Font Style Combinations in vb.net

查看:459
本文介绍了vb.net中的多种字体样式组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想创建一个带有多种样式组合的字体,比如粗体和下划线,我必须在它之间放置'或'语句,如下面的示例所示:

If I want to create a font with multiple style combinations, like bold AND underline, I have to place the 'or' statement between it, like in the example below:

  lblArt.Font = New Font("Tahoma", 18, FontStyle.Bold Or FontStyle.Underline)

如果你放置粗体和下划线,它将不起作用,你只能得到2中的一个(比如说这个或者语句应该如何工作),而这将是在逻辑上做到这一点。这背后的原因是什么?

If you place bold 'and' underline, it won't work, and you only get 1 of the 2 (like how the or statement should be working), while that would be the logically way to do it. What is the reason behind this?

推荐答案

布尔逻辑与我们在英语中使用的术语有所不同。这里发生的事情是,枚举的 FontStyle 值实际上是位标志,为了操作位标志,可以使用按位操作

Boolean logic works a bit differently than the way we use the terms in English. What's happening here is that the enumerated FontStyle values are actually bit flags, and in order to manipulate bit flags, you use bitwise operations.

两个位标志,你或他们在一起。 OR操作组合了这两个值。所以想象 FontStyle.Bold 是2, FontStyle.Underline 是4.当你把它们放在一起的时候,你把他们结合在一起在布尔逻辑中,如果两个操作数中的两个位中的任何一个都可以,则可以将一个OR操作视为返回true( ie ,在结果中设置该位)如果两个操作数中的位没有设置,那么设置为false。

To combine two bit flags, you OR them together. An OR operation combines the two values. So imagine that FontStyle.Bold was 2 and FontStyle.Underline was 4. When you OR them together, you get 6—you've combined them together. In Boolean logic, you can think of an OR operation as returning "true" (i.e., setting that bit in the result) if either of the bits in the two operands are set, and "false" if neither of the bits in the two operands are set.

可以为这样的操作写一个真值表如下所示:

You can write a truth table for such an operation as follows:

| A | B | A OR B |
|---|---|--------|
| 0 | 0 |   0    |
| 0 | 1 |   1    |
| 1 | 0 |   1    |
| 1 | 1 |   1    |

请注意,结果更贴切地反映了我们在非正式英语中所称的和。如果任何一个设置了,那么结果也会被设置。

Notice that the results more closely mirror what we, in informal English, would call "and". If either one has it set, then the result has it set, too.

与OR不同的是,按位AND操作只返回true( ie ,在结果中设置该位),如果两个操作数中的两个位都被设置。否则,结果是假。再次,可以写一个真值表:

In contrast to OR, a bitwise AND operation only returns "true" (i.e., sets that bit in the result) if both of the bits in the two operands are set. Otherwise, the result is "false". Again, a truth table can be written:

| A | B | A AND B |
|---|---|---------|
| 0 | 0 |    0    |
| 0 | 1 |    0    |
| 1 | 0 |    0    |
| 1 | 1 |    1    |

假设 FontStyle.Bold 值2和 FontStyle.Underline 的值为4,如果你和他们在一起,你得到0.这是因为这些值有效地相互抵消。最终的结果是,你没有得到任何字体样式 - 正因如此,当你编写 FontStyle.Bold和FontStyle.Underline

Assuming again that FontStyle.Bold has the value 2 and FontStyle.Underline has the value 4, if you AND them together, you get 0. This is because the values effectively cancel each other out. The net result is that you don't get any font styles—precisely why it doesn't work when you write FontStyle.Bold And FontStyle.Underline.

在VB中,使用运算符执行按位或运算。 操作符执行按位与操作。所以为了做一个按位包含的值,就是如何组合位标志,可以使用或者运算符。

In VB, a bitwise OR operation is performed using the Or operator. The And operator performs a bitwise AND operation. So in order to do a bitwise inclusion of values, which is how you combine bit flags, you use the Or operator.

这篇关于vb.net中的多种字体样式组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆