TabRenderer 类

定义

提供用于呈现具有视觉样式的选项卡控件的方法。 此类不能被继承。

C#
public sealed class TabRenderer
C#
public static class TabRenderer
继承
TabRenderer

示例

下面的代码示例演示如何创建自定义控件,该控件使用 DrawTabPageDrawTabItem 方法通过两个选项卡绘制基本选项卡控件。

C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace TabRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            CustomTabControl Tab1 = new CustomTabControl();
            Controls.Add(Tab1);
            this.Size = new Size(500, 500);
        }

        [STAThread]
        static void Main()
        {
            // The call to EnableVisualStyles below does not affect whether 
            // TabRenderer.IsSupported is true; as long as visual styles 
            // are enabled by the operating system, IsSupported is true.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class CustomTabControl : Control
    {
        private Rectangle tabPageRectangle;
        private Rectangle tabItemRectangle1;
        private Rectangle tabItemRectangle2;
        private int tabHeight = 30;
        private int tabWidth = 100;
        private TabItemState tab1State = TabItemState.Selected;
        private TabItemState tab2State = TabItemState.Normal;
        private string tab1Text = "Tab 1";
        private string tab2Text = "Tab 2";
        private bool tab1Focused = true;
        private bool tab2Focused = false;

        public CustomTabControl()
            : base()
        {
            this.Size = new Size(300, 300);
            this.Location = new Point(10, 10);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "TabRenderer";
            this.DoubleBuffered = true;

            tabPageRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y + tabHeight,
                ClientRectangle.Width,
                ClientRectangle.Height - tabHeight);

            // Extend the first tab rectangle down by 2 pixels, 
            // because it is selected by default.
            tabItemRectangle1 = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y, tabWidth, tabHeight + 2);

            tabItemRectangle2 = new Rectangle(ClientRectangle.Location.X +
                tabWidth, ClientRectangle.Location.Y, tabWidth, tabHeight);
        }

        // Draw the tab page and the tab items.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!TabRenderer.IsSupported)
            {
                this.Parent.Text = "CustomTabControl Disabled";
                return;
            }

            TabRenderer.DrawTabPage(e.Graphics, tabPageRectangle);
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle1,
                tab1Text, this.Font, tab1Focused, tab1State);
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle2,
                tab2Text, this.Font, tab2Focused, tab2State);

            this.Parent.Text = "CustomTabControl Enabled";
        }

        // Draw the selected tab item.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (!TabRenderer.IsSupported)
                return;

            // The first tab is clicked. Note that the height of the 
            // selected tab rectangle is raised by 2, so that it draws 
            // over the border of the tab page.
            if (tabItemRectangle1.Contains(e.Location))
            {
                tab1State = TabItemState.Selected;
                tab2State = TabItemState.Normal;
                tabItemRectangle1.Height += 2;
                tabItemRectangle2.Height -= 2;
                tab1Focused = true;
                tab2Focused = false;
            }

            // The second tab is clicked.
            if (tabItemRectangle2.Contains(e.Location))
            {
                tab2State = TabItemState.Selected;
                tab1State = TabItemState.Normal;
                tabItemRectangle2.Height += 2;
                tabItemRectangle1.Height -= 2;
                tab2Focused = true;
                tab1Focused = false;
            }

            Invalidate();
        }
    }
}

注解

TabRenderer 类提供了一组 static 方法,可用于使用操作系统的当前视觉样式呈现选项卡控件。 呈现控件是指绘制控件的用户界面。 如果要绘制应具有当前视觉样式外观的自定义控件,这非常有用。 若要绘制选项卡控件,请使用 DrawTabPage 该方法绘制页面,并使用 DrawTabItem 该方法绘制每个选项卡。

如果在操作系统中启用了视觉样式,并且视觉样式应用于应用程序窗口的工作区,则此类的方法将使用当前视觉样式绘制选项卡控件。 否则,此类的方法和属性将引发 。InvalidOperationException 若要确定是否可以使用此类的成员,请检查 IsSupported 属性值。

此类包装 System.Windows.Forms.VisualStyles.VisualStyleRenderer 设置为类公开 System.Windows.Forms.VisualStyles.VisualStyleElement.Tab 的元素之一的功能。 有关详细信息,请参阅 使用视觉样式呈现控件

属性

IsSupported

获取一个值,该值指示 TabRenderer 类能否用于绘制具有视觉样式的选项卡控件。

方法

DrawTabItem(Graphics, Rectangle, Boolean, TabItemState)

绘制具有指定的状态和边界以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, Image, Rectangle, Boolean, TabItemState)

绘制具有指定的状态和边界、指定的图像以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, String, Font, Boolean, TabItemState)

绘制具有指定的状态和边界、指定的文本以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, TabItemState)

绘制具有指定的状态和边界、指定的文本和图像以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, String, Font, TabItemState)

绘制具有指定的状态和边界以及指定的文本的选项卡。

DrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, TabItemState)

绘制具有指定的状态和边界、指定的文本和文本格式以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, TabItemState)

绘制具有指定的状态和边界、指定的文本、文本格式和图像以及可选聚焦框的选项卡。

DrawTabItem(Graphics, Rectangle, TabItemState)

绘制具有指定状态和边界的选项卡。

DrawTabPage(Graphics, Rectangle)

绘制具有指定边界的选项卡页。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅