我想知道是否有某种方法可以使 TabControl 的背景透明(TabControl,而不是 TabPages),而不是采用父表单背景颜色。我有一些带有自定义绘画的表单,我在其中绘制了一个渐变作为背景,但这个渐变不会在 tabcontrols 后面绘制。我尝试设置 TabControl.Backcolor = Color.Transparent 但它告诉我它不受支持。我正在使用 VS2005 和框架 2.0。(设置样式无济于事)有人对这个问题有很好的解决方法吗?
Golan
问问题
5523 次
4 回答
3
自定义选项卡控制:
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
protected override void OnPaintBackground(PaintEventArgs e)
{
if (this.BackColor == Color.Transparent)
{
IntPtr hdc = e.Graphics.GetHdc();
Rectangle rec = new Rectangle(e.ClipRectangle.Left,
e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height);
DrawThemeParentBackground(this.Handle, hdc, ref rec);
e.Graphics.ReleaseHdc(hdc);
}
else
{
base.OnPaintBackground(e);
}
}
于 2009-09-06T07:00:55.790 回答
1
根据msdn 上的这个线程,tabcontrol 不支持将背景色更改为透明,但是您应该可以覆盖 drawitem 方法。
于 2009-09-06T06:18:07.207 回答
1
我将扩展答案Golan
(因为他不活跃?)
您可以使用DrawThemeParentBackground完成大部分工作。创建自定义 TabControl:
[System.ComponentModel.DesignerCategory("Code")]
public class MyTabControl : TabControl
{
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
// use with care, as it may cause strange effects
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
public MyTabControl() { }
protected override void OnPaintBackground(PaintEventArgs e)
{
IntPtr hdc = e.Graphics.GetHdc();
Rectangle rect = ClientRectangle;
DrawThemeParentBackground(this.Handle, hdc, ref rect);
e.Graphics.ReleaseHdc(hdc);
}
}
并为每个显式设置TabPage
BackColor=Transparent
(在 Designer 中或在运行时,如果你不这样做 -TabPage
将有白色背景)。就像我梦寐以求的奇迹、透明、无闪烁的 TabControl 一样工作。
于 2013-04-05T07:35:11.483 回答
1
正如MSDN 上所解释的,你应该
- 检查是否
Application.RenderWithVisualStyles
返回真 - 将 TabPage 的
UseVisualStyleBackColor
属性设置为 true - 将 TabControl
Appearance
属性设置为Normal
那么你的 TabPage 应该有一个透明的背景。
于 2015-01-14T11:00:57.447 回答