c# - 没有启用视觉样式的 TabRenderer?

标签 c# .net winforms visual-styles

我想绘制一个具有自定义功能的自定义 TabControl

为此,我继承了 Panel 类并覆盖了 OnPaint 方法以使用 TabRenderer 类进行绘制。

问题是 TabRenderer 仅在启用视觉样式时工作(可以使用 TabRenderer.IsSupported 检查),但如果禁用视觉样式我该怎么办?

在这种情况下,我想到了使用ControlPaint 类来绘制没有视觉样式的选项卡,但是它没有与Tabs 相关的绘制方法。我希望它基本上在视觉上表现得像常规的 TabControl

最佳答案

您必须自己绘制它,因为没有为此发布的 API。希望以非视觉样式的方式做到这一点相对容易。

您可以使用 ControlPaint.DrawBorder3D 绘制 Pane 边框并使用类似于以下代码的按钮:

int Top = bounds.Top;
int Bottom = bounds.Bottom - 1;
int Sign = 1;

if (tabStrip.EffectiveOrientation == TabOrientation.Bottom)
{
    Top = bounds.Bottom - 1;
    Bottom = bounds.Top;
    Sign = -1;
}

using (Pen OuterLightBorderPen = new Pen(SystemColors.ControlLightLight))
{
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left, Bottom, bounds.Left, Top + 2 * Sign);
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left, Top + 2 * Sign, bounds.Left + 2, Top);
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left + 2, Top, bounds.Right - 3, Top);
}

using (Pen InnerLightBorderPen = new Pen(SystemColors.ControlLight))
{
    e.Graphics.DrawLine(InnerLightBorderPen, bounds.Left + 1, Bottom, bounds.Left + 1, Top + 2 * Sign);
    e.Graphics.DrawLine(InnerLightBorderPen, bounds.Left + 2, Top + 1 * Sign, bounds.Right - 3, Top + 1 * Sign);
}

using (Pen OuterDarkBorderPen = new Pen(SystemColors.ControlDarkDark))
{
    e.Graphics.DrawLine(OuterDarkBorderPen, bounds.Right - 2, Top + 1 * Sign, bounds.Right - 1, Top + 2 * Sign);
    e.Graphics.DrawLine(OuterDarkBorderPen, bounds.Right - 1, Top + 2 * Sign, bounds.Right - 1, Bottom);
}

using (Pen InnerDarkBorderPen = new Pen(SystemColors.ControlDark))
    e.Graphics.DrawLine(InnerDarkBorderPen, bounds.Right - 2, Top + 2 * Sign, bounds.Right - 2, Bottom);

关于c# - 没有启用视觉样式的 TabRenderer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577963/

相关文章:

c# - 使用 FluentValidation 验证集合,返回一个属性失败的规则错误

.net - VB.NET: bool 值来自 `Nothing` 有时是 `false` 有时是 Nullreference-Exception

.net - 为什么我的 DataGridView 拒绝显示底部数据以及如何修复它?

C# - 从 ContextMenuStrip 和 "Smart Events"动态添加和删除项目

c# - 从 Windows 窗体过渡到 WPF

c# - 未加载 UWP 项目(需要更新)

c# - 从对 HttpWebRequest 的调用中复制 HTTP 请求/响应 header ?

c# - Visual Studio 2010 的问题

c# - 通过单击任何地方/任何其他地方从 TextBox 中删除焦点

.net - 管道上的调用目标已引发异常