private void menuItemBold_Click(object sender, System.EventArgs e)
{
Font newFont=new Font(rtfText.SelectionFont,
(rtfText.SelectionFont.Bold?
rtfText.SelectionFont.Style&~FontStyle.Bold:
rtfText.SelectionFont.Style|FontStyle.Bold));
rtfText.SelectionFont=newFont;
}
这段代码从RichTextBox的选中文本当前使用的字体中创建一个新字体,如果字体的样式已经设置好了,就从新字体中删除它,否则就在字体中包含该样式。接着把选种文本的字体设置为新字体。现在我不能理解&和|一元符号具体的逻辑。
请大家帮忙指点,谢谢!

解决方案 »

  1.   

    能否解释清楚一点?&和|是两个位运算符,不会就是OR和AND那么简单理解吧?谢谢!
      

  2.   

    确实是的,一般情况下&&和&, ||和|没有区别。
      

  3.   

    FontStyle fs =
    (rtfText.SelectionFont.Bold?
    rtfText.SelectionFont.Style&~FontStyle.Bold:
    rtfText.SelectionFont.Style|FontStyle.Bold)等价于:FontStyle fs;
    if (rtfText.SelectionFont.Bold) 
      fs = rtfText.SelectionFont.Style & ~FontStyle.Bold;
    else 
      fs = rtfText.SelectionFont.Style | FontStyle.Bold;如果原来字体是Bold(加粗)的, 就去掉Bold, 否则就加上Bold.FontStyle.Bold 是一个位标志, FontStyle 枚举共有以下位标志:Regular     0   0000
    Bold        1   0001
    Italic      2   0010
    Underline   4   0100
    Strikeout   8   1000rtfText.SelectionFont.Style & ~FontStyle.Bold 就是清除 Bold 位,
    rtfText.SelectionFont.Style | FontStyle.Bold  就是置 Bold 位.例如 (0101 & ~0001) 的结果是 0100, 而 (0100 | 0001) 的结果是 0101.