2011-10-14 80 views
13

我對每一個tabControls這些視覺故障的時候,我改變了tabPagesBackColor和形式的BackColor,以下圖片所示:的TabControl和邊框的視覺干擾

  • tabPage的頂部,有一個內部的一個像素的白色邊框。
  • tabPage的左側,有一個內部三像素的白色邊框。
  • tabPage的底部,有一個內部單像素白色邊框和一個外部雙像素白色邊框。
  • tabPage的右側,有一個內部一個像素的白色邊框和一個外部兩個像素的白色邊框。

Top and left borders Bottom borders Top and right borders

有沒有一種方法可以讓我擺脫那些白色邊框的?

+0

如果爲父級設置背景色(tabcontrol),會發生什麼情況? – Rob

+0

@Rob:不幸的是,一個'tabControl'沒有BackColor屬性。 – Otiel

+0

奧克,你可以嘗試使用css設置頁面頁邊距。 margin:-1px-3px-3px-3px;它導致字段變大一點,並覆蓋空白 – Rob

回答

15

這是我的企圖破解。我使用NativeWindow來繪製TabControl來填充這些「白色」空間。我不會要求它是完美的:

public class TabPadding : NativeWindow { 
    private const int WM_PAINT = 0xF; 

    private TabControl tabControl; 

    public TabPadding(TabControl tc) { 
    tabControl = tc; 
    tabControl.Selected += new TabControlEventHandler(tabControl_Selected); 
    AssignHandle(tc.Handle); 
    } 

    void tabControl_Selected(object sender, TabControlEventArgs e) { 
    tabControl.Invalidate(); 
    } 

    protected override void WndProc(ref Message m) { 
    base.WndProc(ref m); 

    if (m.Msg == WM_PAINT) { 
     using (Graphics g = Graphics.FromHwnd(m.HWnd)) { 

     //Replace the outside white borders: 
     if (tabControl.Parent != null) { 
      g.SetClip(new Rectangle(0, 0, tabControl.Width - 2, tabControl.Height - 1), CombineMode.Exclude); 
      using (SolidBrush sb = new SolidBrush(tabControl.Parent.BackColor)) 
      g.FillRectangle(sb, new Rectangle(0, 
              tabControl.ItemSize.Height + 2, 
              tabControl.Width, 
              tabControl.Height - (tabControl.ItemSize.Height + 2))); 
     } 

     //Replace the inside white borders: 
     if (tabControl.SelectedTab != null) { 
      g.ResetClip(); 
      Rectangle r = tabControl.SelectedTab.Bounds; 
      g.SetClip(r, CombineMode.Exclude); 
      using (SolidBrush sb = new SolidBrush(tabControl.SelectedTab.BackColor)) 
      g.FillRectangle(sb, new Rectangle(r.Left - 3, 
               r.Top - 1, 
               r.Width + 4, 
               r.Height + 3)); 
     } 
     } 
    } 
    } 
} 

並把它掛:

public Form1() { 
    InitializeComponent(); 
    var tab = new TabPadding(tabControl1); 
} 

我的最終結果是:

enter image description here

+0

看起來非常順利。現在,我覺得它很完美。如果我發現一些故障,我會讓你保持最新狀態。非常感謝,令人驚歎的工作! – Otiel

+2

哇,謝謝你。非常感謝你!究竟是什麼人會期望TabControl做(並再次「微軟,爲什麼?!」) – Knickedi

+0

我無法得到這個解決方案爲我工作。我精確地複製了代碼,並用我現有的tabControl創建了一個新的TabPad - 它什麼都沒做。 – stuzor

3

可以從控制繼承

public class TabControlEx : TabControl 
{ 
    protected override void WndProc(ref Message m) 
    { 
     if (m.Msg == 0x1300 + 40) 
     { 
      RECT rc = (RECT)m.GetLParam(typeof(RECT)); 
      rc.Left -= 0; 
      rc.Right += 3; 
      rc.Top -= 0; 
      rc.Bottom += 3; 
      Marshal.StructureToPtr(rc, m.LParam, true); 
     } 
     base.WndProc(ref m); 
    } 

} 
internal struct RECT { public int Left, Top, Right, Bottom; } 
+1

這是此頁面上唯一適用於我的解決方案,但是我必須將左對齊調整爲 - = 4,右對齊爲+ = 2,以及頂端對 - = 2.謝謝 – stuzor

0

我最近遇到這個問題,從來沒有找到一個好的簡單解決方案。那時候我只想簡單地調整控件的裁剪區域。這似乎工作得很好,沒有明顯的副作用。右側的兩個額外的白色像素和底部的一個額外的白色像素不再可見。

class TabControlEx : TabControl 
{ 
    protected override void WndProc(ref Message m) 
    { 
     if (m.Msg == 0x0005) // WM_SIZE 
     { 
      int Width = unchecked((short)m.LParam); 
      int Height = unchecked((short)((uint)m.LParam >> 16)); 

      // Remove the annoying white pixels on the outside of the tab control 
      // by adjusting the control's clipping region to exclude the 2 pixels 
      // on the right and one pixel on the bottom. 
      Region = new Region(new Rectangle(0, 0, Width - 2, Height - 1)); 
     } 

     base.WndProc(ref m); 
    } 
} 
+0

我無法使用此解決方案爲我工作: - ( – stuzor

-1

使用MaterialSkin 從NuGet包添加MaterialSkin [您的解決方案右鍵單擊 - 單擊管理的NuGet包 - 搜索MaterialSkin.dll] 下一阻力MaterialSkin.dll在你的工具箱 和使用材料外殼的tabcontrol

+0

我不想使用Google的Material Design,只是簡單的WinForms。 – Otiel