在重寫TabControl的時候我們最先想到的是設定
this.DrawMode = TabDrawMode.OwnerDrawFixed;
然後重寫
protected override void OnDrawItem(DrawItemEventArgs e){ base.OnDrawItem(e);}
這樣重寫後只是重寫選項卡上的地區,這個地區並不包括邊框,這樣我們所重寫的和邊框不搭調,也無法達到我們想要的功能 。
而更好的方法是重寫整個控制項
設定控制項由使用者繪製
private void SetStyles() { base.SetStyle( ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true); base.UpdateStyles(); }
關鍵的地方在 ControlStyles.UserPaint 設定為true
重寫OnPaint
protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias; pe.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; // pe.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias; //繪製背景 Rectangle rect = this.ClientRectangle; pe.Graphics.FillRectangle(new TextureBrush(Properties.Resources.bg), rect); //繪製邊線 int height = this.ItemSize.Height+3; Rectangle r = new Rectangle(DisplayRectangle.X - 1, DisplayRectangle.Y - 1, DisplayRectangle.Width + 1, DisplayRectangle.Height + 1); // pe.Graphics.DrawLine(new Pen(Color.FromArgb(157, 162, 168)), new Point(rect.X, rect.Y + height), new Point(rect.Right, rect.Y + height)); pe.Graphics.DrawRectangle(new Pen(_lineColor), r); //繪製邊框 //繪製標題 foreach (TabPage tp in this.TabPages) { DrawTabPage(pe.Graphics, this.GetTabRect(this.TabPages.IndexOf(tp)),tp); } }
this.GetTabRect函數可以擷取的標籤的位置,這個為我們重繪標籤提供了很好的協助
重繪標籤
private void DrawTabPage(Graphics graphics, Rectangle rectangle, TabPage tp) { //繪製底紋 StringFormat sf = new StringFormat(); sf.Trimming = StringTrimming.EllipsisCharacter; sf.FormatFlags = StringFormatFlags.NoWrap; Rectangle rect = new Rectangle (rectangle.X ,rectangle .Y +2,rectangle .Width ,rectangle .Height -2); //標準地區 Rectangle fontRect = new Rectangle(rectangle.X + 5, rectangle.Y+5, rectangle.Width - 10, tp .Font .Height);//文字地區 if (this.SelectedTab.Equals(tp)) { rect = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); fontRect = new Rectangle(rectangle.X + 5, rectangle.Y + 5, rectangle.Width - 25, rectangle.Height - 7); //繪製邊框 graphics.FillPath(new LinearGradientBrush(new Point(rect.X, rect.Y), new Point(rect.X, rect.Y + rect.Height), _ColorActivateA, _ColorActivateB), CreateTabPath(rect)); //填充顏色 graphics.DrawString(tp.Text, tp.Font, new SolidBrush(tp.ForeColor), fontRect, sf); //文字繪製 graphics.DrawPath(new Pen(_lineColor), CreateTabPath(rect));//繪製實邊線 //掩蓋下部的繪製 graphics.DrawLine(new Pen(_ColorActivateB, 3), rect.X, rect.Bottom + 1, rect.X + rect.Width, rect.Bottom + 1); //繪製圖片 Rectangle rectClose = GetCloseRect(rectangle); } else { //繪製邊框 graphics.FillPath(new LinearGradientBrush (new Point (rect .X,rect .Y ),new Point (rect .X,rect .Y +rect .Height ),_ColorDefaultA,_ColorDefaultB), CreateTabPath(rect)); //填充顏色 graphics.DrawString(tp.Text, tp.Font, new SolidBrush(tp.ForeColor), fontRect, sf); //文字繪製 // graphics.DrawPath(new Pen(Color.Wheat, 2), CreateTabPath(rect));//繪製高光邊線 graphics.DrawPath(new Pen(_lineColor), CreateTabPath(rect));//繪製實邊線 }
繪製關閉標籤按鈕
我們給標籤加上了一個滑鼠移上去顯示關閉按鈕的功能 重寫OnMouseMove的功能
bool CloseEX = false; protected override void OnMouseMove(MouseEventArgs e) { if (TabPageMouseClose(e.Location) != CloseEX) { CloseEX = !CloseEX; this.Invalidate(); } base.OnMouseMove(e); }
在滑鼠移動過快的時候,會導致關閉按鈕沒有重新整理,所有我們要在滑鼠離開控制項的時候讓CloseEx=False並重繪製。
好到這一步我們就完成我我們的繪製工作 。我在繪製的時候使用了漸層畫刷用了5個參數來標識每個指定的顏色
private Color _lineColor = Color.FromArgb(157, 162, 168); //邊線顏色 private Color _ColorDefaultA = Color.FromArgb(231, 231, 231);//預設標籤漸層 aprivate Color _ColorDefaultB = Color.FromArgb(255, 255, 255);//預設標籤漸層 bprivate Color _ColorActivateA = Color.FromArgb(184, 203, 217);//點擊漸層aprivate Color _ColorActivateB = Color.FromArgb(255, 255, 255);//點擊漸層a