Oti*_*iel 14 c# tabcontrol winforms
tabControls每当我更改它tabPages BackColor和BackColor表单时,我都会遇到这些视觉故障,如下图所示:
tabPage,有一个内部单像素白色边框.tabPage,有一个内部三像素白色边框.tabPage,有一个内部单像素白色边框和一个外部双像素白色边框.tabPage,有一个内部单像素白色边框和一个外部两像素白色边框.

有没有办法摆脱那些白色边界?
Lar*_*ech 17
这是我的黑客攻击.我用a 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));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
把它连接起来:
public Form1() {
InitializeComponent();
var tab = new TabPadding(tabControl1);
}
Run Code Online (Sandbox Code Playgroud)
我的最终结果:

你可以从控制继承
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; }
Run Code Online (Sandbox Code Playgroud)