本文介绍了的TabControl和边框的视觉干扰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对每个这些视觉毛刺的TabControls
时,我改变了的TabPages
背景色
和背景色
的形式,对下面的图像所示:
I have these visual glitches on every tabControls
when I am changing its tabPages
BackColor
and the BackColor
of the form, as illustrated on the following images:
- 在
Tab页
,有一个内部的一个像素的白色边框。 顶部 - 在的
Tab页
,有一个内部的三个像素白色边框的左侧。 - 在底部的
Tab页
,有一个内部的一个像素的白色边框和外部两个像素的白色边框。 - 在右侧的
Tab页
,有一个内部的一个像素的白色边框和外部两个像素的白色边框。
- At the top of the
tabPage
, there is an interior one-pixel white border. - At the left of the
tabPage
, there is an interior three-pixels white border. - At the bottom of the
tabPage
, there is an interior one-pixel white border and an exterior two-pixels white border. - At the right of the
tabPage
, there is an interior one-pixel white border and an exterior two-pixels white border.
有没有一种方法可以让我摆脱那些白色边框的?
Is there a way I can get rid of those white borders?
推荐答案
下面是我的企图的黑客。我用了一个的NativeWindow
绘制在的TabControl
填写的白的空间。我不会声称它是完美的:
Here's my attempted hack. I used a NativeWindow
to draw over the TabControl
to fill in those "white" spaces. I won't claim it's perfect:
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);
}
我的最终结果是:
My end result:
这篇关于的TabControl和边框的视觉干扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!