如何更改 C# winforms 中未使用空间选项卡的背景颜

How to change the background color of unused space tab in C# winforms?(如何更改 C# winforms 中未使用空间选项卡的背景颜色?)
本文介绍了如何更改 C# winforms 中未使用空间选项卡的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  |Tab1|Tab2|Tab3| {    }
  |                     |
  |                     |
  |                     |
  |                     |
  |_____________________|

我可以改变 Tabbackcolorforecolor.. 但我想改变那个 { } 的颜色 --> 空白空间可以做到这一点...它显示默认的winforms颜色..帮助我在dis..

I am able to change the backcolor and forecolor of Tab.. but I want to change the color of that { } -- > Empty space is this possible to do that. .. It shows default winforms color..help me in dis..

 private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if ( e.Index == this.tabControl1.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName  = this.tabControl1.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle  recTab = e.Bounds;
        recTab = new Rectangle( recTab.X,  recTab.Y + 4,  recTab.Width,  recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);

    }

推荐答案

我认为给那个空间一个颜色的唯一方法是覆盖窗口的 OnPaintBackground 方法,所以把这个粘贴到你的表单(窗口)

I think the only way to give that space a color is to override the OnPaintBackground method of the window, so just paste this on your form (window)

您还必须将外观属性更改为正常"

you must also change the Appearance Property to "Normal"

private void Form1_Load(object sender, EventArgs e)
{

}

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
    RectangleF emptyspacerect = new RectangleF(
            lasttabrect.X + lasttabrect.Width + tabControl1.Left,
            tabControl1.Top + lasttabrect.Y, 
            tabControl1.Width - (lasttabrect.X + lasttabrect.Width), 
            lasttabrect.Height);

    Brush b = Brushes.BlueViolet; // the color you want
    e.Graphics.FillRectangle(b, emptyspacerect );
}

对我来说效果很好

这篇关于如何更改 C# winforms 中未使用空间选项卡的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)