2010-02-24 109 views
4

我在C#Windows應用程序中有一些tabControl。它有一些tabPages。有沒有人kwows如何使tabPage文本變得粗體..?如何使TabPage的標題文字變爲粗體?

+4

WPF or Winform? – 2010-02-24 13:51:20

+0

你想大膽做些什麼?標籤標題或標籤頁的內容? – 2010-02-24 13:54:05

+0

Winform。我想大膽的標題標題。 – Vytas999 2010-02-24 14:28:08

回答

9
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) 
     { 

      Graphics g = e.Graphics; 
      Brush _TextBrush; 

      // Get the item from the collection. 
      TabPage _TabPage = tabControl1.TabPages[e.Index]; 

      // Get the real bounds for the tab rectangle. 
      Rectangle _TabBounds = tabControl1.GetTabRect(e.Index); 

      if (e.State == DrawItemState.Selected) 
      { 
       // Draw a different background color, and don't paint a focus rectangle. 
       _TextBrush = new SolidBrush(Color.Blue); 
       g.FillRectangle(Brushes.Gray, e.Bounds); 
      } 
      else 
      { 
       _TextBrush = new System.Drawing.SolidBrush(e.ForeColor); 
       // e.DrawBackground(); 
      } 

      // Use our own font. Because we CAN. 
      Font _TabFont = new Font(e.Font.FontFamily, (float)9, FontStyle.Bold, GraphicsUnit.Pixel); 
      //Font fnt = new Font(e.Font.FontFamily, (float)7.5, FontStyle.Bold); 

      // Draw string. Center the text. 
      StringFormat _StringFlags = new StringFormat(); 
      _StringFlags.Alignment = StringAlignment.Center; 
      _StringFlags.LineAlignment = StringAlignment.Center; 
      g.DrawString(tabControl1.TabPages[e.Index].Text, _TabFont, _TextBrush, 
         _TabBounds, new StringFormat(_StringFlags)); 

     } 
+0

只需添加到:受影響的控件的'DrawMode'應設置爲'TabDrawMode.OwnerDrawFixed',這使得使用'DrawItem'事件是強制性的(如果您將此方法的代碼留空,則標籤的標題欄也將爲空白) – DrCopyPaste 2016-05-03 07:56:00

3

在Winforms中,您可以更改DrawMode並繪製自己的所有標題。

查看MSDN Example

相關問題