TabRenderer 没有启用视觉样式?

发布于 2024-08-27 21:10:48 字数 345 浏览 10 评论 0原文

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

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

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

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

I want to draw a custom TabControl with custom functionality.

To do this, i inherited the Panel class and overrided OnPaint method to draw with TabRenderer class.

The problem is that TabRenderer working only when visual styles enabled (can be checked with TabRenderer.IsSupported), but what should i do if visual styles disabled?

In this case, I thought using the ControlPaint class to draw tabs without visual styles, but it has no draw methods related to Tabs. I want it basically to behave visually like the regular TabControl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2024-09-03 21:10:48

你必须自己绘制它,因为没有公开的API。希望以非视觉风格的方式相对容易做到这一点。

您可以使用 ControlPaint.DrawBorder3D 并对按钮使用类似以下代码:

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);

You have to draw it by yourself, because there is not published API for this. Hopefully this is relatively easy to do it in non-visualstyles way.

You can draw pane border with ControlPaint.DrawBorder3D and use something like the following code for buttons:

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);
行雁书 2024-09-03 21:10:48

这是一个“外面”的答案,但是您可以使用 wpf 吗?正如您从上面的答案中看到的那样,在 winform 中自定义控件是一件很痛苦的事情,而在 WPF 中,每个控件都是不好看的。这意味着您可以完全控制渲染的内容及其外观。

This is an "out there" answer but is it possible that you could use wpf? As you can see from the answer above it is a pain in the ear to customise controls in winforms where as in WPF each control is lookless. This means that you control what is rendered and how it looks completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文