Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选项卡根据文件名控制itemsize_C#_Winforms_Tabcontrol - Fatal编程技术网

C# 选项卡根据文件名控制itemsize

C# 选项卡根据文件名控制itemsize,c#,winforms,tabcontrol,C#,Winforms,Tabcontrol,我需要tabcontrol itemsize的帮助。(winforms) 我有一个业主绘制的控制和所有工作良好。在tabcontrol属性中,Itemsize设置为width 0,sizemode设置为fixed。当我打开一个文件名如test.txt时,“按钮”看起来像这样 |test123.txt | |Untitled | 这很好,但是,如果我打开更长的文件名,那么“按钮”如下所示 |thisisaverylongfilename.txt

我需要tabcontrol itemsize的帮助。(winforms)

我有一个业主绘制的控制和所有工作良好。在tabcontrol属性中,Itemsize设置为width 0,sizemode设置为fixed。当我打开一个文件名如test.txt时,“按钮”看起来像这样

|test123.txt    | 
|Untitled    |
这很好,但是,如果我打开更长的文件名,那么“按钮”如下所示

|thisisaverylongfilename.txt                             |
|anotherlongname.txt                              |
我正在尝试制作“按钮”,以便文件名正好与右侧的一些空间相匹配,并且所有文件的空间都应该相同

下面是我为用户绘制的选项卡控件编写的代码

        Image cross = imageList1.Images[0];
        int xWidth = cross.Width;

        TabPage tp = tabControl1.TabPages[e.Index];

        Size size = tabControl1.ItemSize;

        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if (e.Index == this.tabControl1.SelectedIndex)
        {
            fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromName(tabpages_primary_backcolor), Color.FromName(tabpages_secondary_backcolor), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Firebrick;
        }
        else
        {
            fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular);

            bshBack = new SolidBrush(Color.Empty);
            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 + 5, recTab.Y + 4, recTab.Width, recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);


        if (tabControl1.SelectedTab != null)
        {
            currentCrossRect = new Rectangle(
            e.Bounds.Left + size.Width - 22, 3, xWidth, xWidth);
            e.Graphics.DrawImage(cross, currentCrossRect.X - 2, currentCrossRect.Y + 2);
        }

感谢和问候

使用以下方法获取文本宽度:

SizeF s = e.Graphics.MeasureString(myString, myFont);

然后您可以将项目的宽度设置为s.width

是的,对不起-我指的是所有者绘制的,它是为winforms设计的。嗨,Galister。在我发布这个问题之前,我看了一下measurestring,但是,我不太确定在我的代码中将宽度设置为s.width的位置,因为宽度是从tabcontrol属性获得的。如果我添加tabControl1.ItemSize=新大小(s.Width,25),则tab按钮会不停地闪烁,宽度不会发生任何变化。