2012-08-08 58 views
4

可能重複:
How can I remove the border padding on container controls in WinForms?刪除周圍的WinForms Tab控件填充

我已經開發了Visual Studio 2008中WinForms應用程序在主窗體我有一個標籤控件。現在我正在嘗試使用背景圖片到標籤頁。我遇到的問題是選項卡控件似乎圍繞它有一個很厚的邊框。此外,選項卡控件不會覆蓋整個表單,而在表單和選項卡頁面之間的頂部留下一行空格。 (我有標籤頁對齊設置在底部)。所以圍繞選項卡控件的邊框和頁面頂部的空白處讓我的頁面看起來很難看。我試圖給與形成背景相同的圖像,但播放spoilsport的選項卡控件填充。

任何想法,使我的設計更好,將不勝感激。

ScreenShot

+5

截圖將幫助很多**。 – CodeCaster 2012-08-08 11:07:59

+0

繼承該控件並按照您希望的方式繪製它。這就是人們如何能夠擁有不是正方形和/或矩形的按鈕。我不明白投訴。簡單的解決方案是設計你自己的沒有這個邊框的控件。 – 2012-08-08 11:22:05

+2

哦!你如何做到這一點?更多的細節請 – Cdeez 2012-08-08 11:23:55

回答

2

我同意大多數這裏提出的意見。標準的TabControl在微軟的部分上畫得很差......即使在Windows Vista/7中,它看起來也不太好!您最好通過繼承TabControl編寫自己的自定義實現,然後繪製所需的其他內容。

您可能會考慮將其用作新控件的模板。你只需要添加一些很酷的設計/繪圖工作到OnPaint和OnPaintBackground方法。

namespace CustomControls 
{ 
    #region USING 

    using System; 
    using System.ComponentModel; 
    using System.Drawing; 
    using System.Windows.Forms; 

    #endregion 

    public class CustomTabControl : TabControl 
    { 
     #region VARIABLES 

     private int hotTrackTab = -1; 

     #endregion 

     #region INSTANCE CONSTRUCTORS 

     public CustomTabControl() : base() 
     { 
      this.InitializeComponent(); 
     } 

     #endregion 

     #region INSTANCE METHODS 

     private void InitializeComponent() 
     { 
      this.SetStyle(ControlStyles.UserPaint, true); 
      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
      this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
      this.SetStyle(ControlStyles.ResizeRedraw, true); 
      this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 

      this.DrawMode = TabDrawMode.OwnerDrawFixed; 
     } 

     private int GetTabUnderCursor() 
     { 
      Point cursor = this.PointToClient(Cursor.Position); 
      for (int index = 0; index < this.TabPages.Count; index++) 
      { 
       if (this.GetTabRect(index).Contains(cursor)) 
       { 
        return index; 
       } 
      } 
      return -1; 
     } 

     private void UpdateHotTrack() 
     { 
      int hot = GetTabUnderCursor(); 
      if (hot != this.hotTrackTab) 
      { 
       if (this.hotTrackTab != -1) 
       { 
        this.Invalidate(this.GetTabRect(this.hotTrackTab)); 
       } 
       this.hotTrackTab = hot; 
       if (this.hotTrackTab != -1) 
       { 
        this.Invalidate(this.GetTabRect(this.hotTrackTab)); 
       } 
       this.Update(); 
      } 
     } 

     #endregion 

     #region OVERRIDE METHODS 

     protected override void OnMouseEnter(EventArgs e) 
     { 
      base.OnMouseEnter(e); 
      this.UpdateHotTrack(); 
     } 

     protected override void OnMouseLeave(EventArgs e) 
     { 
      base.OnMouseLeave(e); 
      this.UpdateHotTrack(); 
     } 

     protected override void OnMouseMove(MouseEventArgs e) 
     { 
      base.OnMouseMove(e); 
      this.UpdateHotTrack(); 
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 
      switch (this.Alignment) 
      { 
       case TabAlignment.Bottom: 
       case TabAlignment.Left: 
       case TabAlignment.Right: 
       case TabAlignment.Top: 
       default: 
        throw new NotImplementedException(); 
      } 
     } 

     protected override void OnPaintBackground(PaintEventArgs pevent) 
     { 
      base.OnPaintBackground(pevent); 
     } 

     #endregion 
    } 
} 

請記住,上面的代碼繪製了一個絕對空白的TabControl,它只顯示DisplayRectangle。其他一切包括你需要自己做的標籤!

此外,要爲單個TabPages繪製背景,您可能還需要重寫和自定義實現TabPage,但是您可能只需使用自定義選項卡控件即可實現所需的結果。

檢查了這一點 http://www.codeproject.com/Articles/42046/Customized-TabControl-by-Repainting-Microsoft-s-Pa

恕我直言,這是更好的... ... http://www.codeproject.com/Articles/38014/KRBTabControl

恕我直言,這是更好... http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition

也期待VB的論壇...我知道我看起來有一些很棒的自定義選項卡控件!