2010-06-25 88 views
2

嘿,我一直在試圖畫我自己的TabControl來擺脫3D陰影,但我沒有太大的成功。 DrawItem事件此刻不會觸發。我必須自己拍攝嗎?我怎麼做?TabControl.DrawItem不會觸發用戶繪製的TabControl

代碼:

namespace NCPad 
{ 
    public partial class NCE_TabControl : TabControl 
    { 
     Rectangle TabBoundary; 
     RectangleF TabTextBoundary; 

     public NCE_TabControl() 
     { 
      InitializeComponent(); 
      this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); 
      this.DrawMode = TabDrawMode.OwnerDrawFixed; 
      this.Paint += new PaintEventHandler(this.OnPaint); 
      this.DrawItem += new DrawItemEventHandler(this.OnDrawItem); 
     } 

     protected void OnPaint(object sender, PaintEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      g.FillRectangle(new SolidBrush(Color.Red), e.ClipRectangle); 
     } 

     protected void OnDrawItem(object sender, DrawItemEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      g.FillRectangle(new SolidBrush(Color.Blue), this.TabBoundary); 
      MessageBox.Show("hi"); 
     } 

     protected override void OnLayout(LayoutEventArgs levent) 
     { 
      base.OnLayout(levent); 

      this.TabBoundary = this.GetTabRect(0); 
      this.TabTextBoundary = (RectangleF)this.GetTabRect(0); 
     } 
    } 
} 
+0

我試圖調用(),但我給自己買了一個不錯的異常。 :( – 2010-06-25 03:37:53

+0

現在我試圖在OnPaint事件過程中遍歷TabPages並使它們失效,但它仍然沒有引發DrawItem事件 – 2010-06-25 04:00:05

+0

如果我添加自己的事件並在循環訪問標籤頁時提高它,但它工作正常但 – 2010-06-25 20:22:58

回答

5

我不知道這一點,但我相信如果你指定ControlStyles.UserPaint位爲true,那麼DrawItem不會觸發。然而,其他ControlStyles(AllPaintingInWmPaint和DoubleBuffer)具有相互依賴關係,因此您也需要將它們關閉。但是,不將UserPaint位設置爲true將導致Paint事件不被觸發。我一直在做的是重寫OnPaintBackground方法:

public partial class NCE_TabControl : TabControl 
{ 
    Rectangle TabBoundary; 
    RectangleF TabTextBoundary; 
    StringFormat format = new StringFormat(); //for tab header text 

    public NCE_TabControl() 
    { InitializeComponent();   
     this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); 
     this.DrawMode = TabDrawMode.OwnerDrawFixed; 
     this.format.Alignment = StringAlignment.Center; 
     this.format.LineAlignment = StringAlignment.Center; 
    } 

    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 
     Graphics g = pevent.Graphics; 
     g.FillRectangle(new SolidBrush(Color.Red), 0, 0, this.Size.Width, this.Size.Height); 

     foreach (TabPage tp in this.TabPages) 
     { 
      //drawItem 
      int index = this.TabPages.IndexOf(tp); 

      this.TabBoundary = this.GetTabRect(index); 
      this.TabTextBoundary = (RectangleF)this.GetTabRect(index); 

      g.FillRectangle(new SolidBrush(Color.LightBlue), this.TabBoundary); 
      g.DrawString("tabPage " + index.ToString(), this.Font, new SolidBrush(Color.Black), this.TabTextBoundary, format); 
     } 
    } 
} 

我認爲這會爲你工作,但也有可能這樣做還有其他方法。

+0

不要忘了處理刷子和StringFormat,否則手柄會泄漏,直到GC啓動 – drake7707 2013-09-26 11:49:36

0

一個簡單的方法是到TabControl的外觀屬性更改爲按鈕FlatButtonsDrawMode badk設置爲正常