【问题标题】:Styling Windows Form Tab样式化 Windows 窗体选项卡
【发布时间】:2020-11-08 04:13:45
【问题描述】:

我正在创建 Windows 选项卡式应用程序。一切都很好,但标签安静地褪色,边框非常暗淡。我也尝试将边框样式更改为 3D,但没有效果。下面是截图

在一些论坛上,人们建议使用第三方库来制作 Google Chrome 类型的标签。但我希望以原生方式获得漂亮的标签。

【问题讨论】:

标签: c# winforms


【解决方案1】:

您可以通过设置 DrawMode = TabDrawMode.OwnerDrawFixed 来控制选项卡的绘制方式。下面的示例假设您在表单上有一个名为 tabControl1 的 TabControl,这将添加一个带有蓝色框的新选项卡。

private Rectangle myTabRect;
        private Rectangle myInsideRect;
        private Rectangle myOutsideRect;
        
        public Form1()
        {
            InitializeComponent();

            TabPage tabPage1 = new TabPage();

            // Sets the tabs to be drawn by the parent window Form1.
            // OwnerDrawFixed allows access to DrawItem. 
            tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            tabControl1.Controls.Add(tabPage1);
            tabControl1.Location = new Point(25, 25);
            tabControl1.Size = new Size(250, 250);
            tabPage1.TabIndex = 0;

            myTabRect = tabControl1.GetTabRect(0);
            myInsideRect = new Rectangle(tabControl1.DisplayRectangle.X -1, tabControl1.DisplayRectangle.Y -1, tabControl1.DisplayRectangle.Width + 1, tabControl1.DisplayRectangle.Height + 1);
            myOutsideRect = tabControl1.ClientRectangle;
            myOutsideRect.Width--;
            myOutsideRect.Height--;

            ClientSize = new Size(300, 500);
            Controls.Add(tabControl1);
            tabControl1.DrawItem += new DrawItemEventHandler(OnDrawItem);
        }

        private void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            // Draw your tab graphics here
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue);
            g.DrawRectangle(p, myTabRect);

            p = new Pen(Color.Red);
            g.DrawRectangle(p, myInsideRect);

            p = new Pen(Color.Green);
            g.DrawRectangle(p, myOutsideRect);
            
        }

您可以在图形上下文中绘制任何您喜欢的样式,添加文本、图像等

【讨论】:

  • 能否请您粘贴标签的外观图片?
  • 看起来不漂亮,但是会附上例子的输出
  • 更新代码以在渲染区域内绘制框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多