首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 程式設計利器之二:結構與列舉——FontStyle 列舉實際應用

     編輯:關於C語言

.NET Framework 列舉基底類別

System.Enum:抽象類別,繼承 ValueType、實作 IComparable、IFormattable、IConvertible。

簡易文字編輯器範例

文字編輯器介面

使用 FontStyle 列舉設定字型樣式:

[Flags]
public enum FontStyle {
    Regular = 0, Bold = 1, Italic = 2, Underline = 4, Strikeout = 8
}

核心程式碼

private void SetStyle(object sender, EventArgs e) {
    ToolStripButton btn = sender as ToolStripButton;
    FontStyle fontStyleContent = this.rchTxtContent.SelectionFont.Style;
    FontStyle btnFont = (FontStyle)Enum.Parse(typeof(FontStyle), btn.Tag.ToString());
    if ((fontStyleContent | btnFont) == fontStyleContent) {
        fontStyleContent = ~btnFont & fontStyleContent; // 移除樣式
    } else {
        fontStyleContent = fontStyleContent | btnFont;  // 加入樣式
    }
    this.rchTxtContent.SelectionFont = new Font(
        this.rchTxtContent.SelectionFont.FontFamily,
        this.rchTxtContent.SelectionFont.Size,
        fontStyleContent,
        this.rchTxtContent.SelectionFont.Unit);
}

利用位元運算切換字型樣式(加粗、斜體、底線、刪除線)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved