我几乎成功地在tabcontrol上画了一个tabitem边框 [英] I almost succed to draw a border on tabitem in tabcontrol

查看:368
本文介绍了我几乎成功地在tabcontrol上画了一个tabitem边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好!



我试图在tabcontrol中设置tabitems的样式。我成功使用下面的代码(tabControl13_DrawItem)



但是。正如您在图像中看到的那样。标签周围还有白色边框,黑色箭头指向图片中。我想知道我们如何为这个边框着色?





为红色边框着色的代码是:

 var recTab2 = new Rectangle(e.Bounds.X + 0,e.Bounds.Y + 2,e.Bounds.Width,e.Bounds.Height  -  2); 
ControlPaint.DrawBorder(e.Graphics,recTab2,Color.Red,ButtonBorderStyle.Solid);





完整代码是:

 private void tabControl13_DrawItem(object sender,DrawItemEventArgs e)
{
//调用此事件处理程序仅当:
// DrawMode = OwnerDrawFixed

var bshBack = new LinearGradientBrush(
e.Bounds,
Color.White,
Color.LightSteelBlue,
LinearGradientMode.ForwardDiagonal);
e.Graphics.FillRectangle(bshBack,e.Bounds);



//也绘制文本
var fntTab = e.Font;
var bshFore = new SolidBrush(Color.Black);
string tabName = this.tabControl13.TabPages [e.Index] .Text;
var sftTab = new StringFormat();
var recTab = new Rectangle(e.Bounds.X + 37,e.Bounds.Y + 13,e.Bounds.Width,e.Bounds.Height - 13);
e.Graphics.DrawString(tabName,fntTab,bshFore,recTab,sftTab);

var recTab2 = new Rectangle(e.Bounds.X + 0,e.Bounds.Y + 2,e.Bounds.Width,e.Bounds.Height - 2);
ControlPaint.DrawBorder(e.Graphics,recTab2,Color.Red,ButtonBorderStyle.Solid);
}







解决方案

Hi Silvers2,


>> 标签周围还有白色边框,黑色箭头指向图片中。我想知道我们如何为这个边框着色?


你可以为白色区域绘制一个矩形:

 Rectangle tabArea = tabControl1.GetTabRect(0); 
e.Graphics.DrawRectangle(new Pen(Color.Blue),tabArea);

请记住在表单加载事件中添加以下代码:

 tabControl1.SizeMode = TabSizeMode.Fixed; 

所以tabControl1_DramItem事件中的完整代码:

 private void tabControl1_DrawItem(object sender,DrawItemEventArgs e)
{
//仅在以下情况下调用此eventhandler:
// DrawMode = OwnerDrawFixed

var bshBack = new LinearGradientBrush(
e.Bounds,
Color.White,
Color.LightSteelBlue,
LinearGradientMode.ForwardDiagonal);
e.Graphics.FillRectangle(bshBack,e.Bounds);

//也绘制文本
var fntTab = e.Font;
var bshFore = new SolidBrush(Color.Black);
string tabName = this.tabControl1.TabPages [e.Index] .Text;
var sftTab = new StringFormat();
var recTab = new Rectangle(e.Bounds.X + 37,e.Bounds.Y + 13,e.Bounds.Width,e.Bounds.Height - 13);
e.Graphics.DrawString(tabName,fntTab,bshFore,recTab,sftTab);

Rectangle tabArea = tabControl1.GetTabRect(0);
e.Graphics.DrawRectangle(new Pen(Color.Blue),tabArea);

var recTab2 = new Rectangle(e.Bounds.X + 0,e.Bounds.Y + 2,e.Bounds.Width,e.Bounds.Height - 2);
ControlPaint.DrawBorder(e.Graphics,recTab2,Color.Red,ButtonBorderStyle.Solid);
}


注意:当您将焦点切换到另一个tabPages时,将出现蓝色矩形。


问候,


Frankie


Hello!

I am trying to style the tabitems in a tabcontrol. I do succeed with the below code in (tabControl13_DrawItem)

However. As you can see in the image. There are white borders around the tab still where the black arrow points in the picture. I wonder how we can color this border as well?

The code that colors the red border is:

var recTab2 = new Rectangle(e.Bounds.X + 0, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2);
            ControlPaint.DrawBorder(e.Graphics, recTab2, Color.Red, ButtonBorderStyle.Solid);


Complete code is:

        private void tabControl13_DrawItem(object sender, DrawItemEventArgs e)
        {
            //this eventhandler is called only if:
            //DrawMode = OwnerDrawFixed
            
            var bshBack = new LinearGradientBrush(
                    e.Bounds,
                    Color.White,
                    Color.LightSteelBlue,
                    LinearGradientMode.ForwardDiagonal);
            e.Graphics.FillRectangle(bshBack, e.Bounds);



            //also draw the text
            var fntTab = e.Font;
            var bshFore = new SolidBrush(Color.Black);
            string tabName = this.tabControl13.TabPages[e.Index].Text;
            var sftTab = new StringFormat();
            var recTab = new Rectangle(e.Bounds.X + 37, e.Bounds.Y + 13, e.Bounds.Width, e.Bounds.Height - 13);
            e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);

            var recTab2 = new Rectangle(e.Bounds.X + 0, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2);
            ControlPaint.DrawBorder(e.Graphics, recTab2, Color.Red, ButtonBorderStyle.Solid);
        }


解决方案

Hi Silvers2,

>>There are white borders around the tab still where the black arrow points in the picture. I wonder how we can color this border as well?

You can draw a rectangle for the white area:

    Rectangle tabArea = tabControl1.GetTabRect(0);
    e.Graphics.DrawRectangle(new Pen(Color.Blue), tabArea);

Remember to add the following code in the form load event:

    tabControl1.SizeMode = TabSizeMode.Fixed;

So the complete code in the tabControl1_DramItem event:

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            //this eventhandler is called only if:
            //DrawMode = OwnerDrawFixed

            var bshBack = new LinearGradientBrush(
                    e.Bounds,
                    Color.White,
                    Color.LightSteelBlue,
                    LinearGradientMode.ForwardDiagonal);
            e.Graphics.FillRectangle(bshBack, e.Bounds);

            //also draw the text
            var fntTab = e.Font;
            var bshFore = new SolidBrush(Color.Black);
            string tabName = this.tabControl1.TabPages[e.Index].Text;
            var sftTab = new StringFormat();
            var recTab = new Rectangle(e.Bounds.X + 37, e.Bounds.Y + 13, e.Bounds.Width, e.Bounds.Height - 13);
            e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
            
            Rectangle tabArea = tabControl1.GetTabRect(0);
            e.Graphics.DrawRectangle(new Pen(Color.Blue), tabArea);

            var recTab2 = new Rectangle(e.Bounds.X + 0, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2);
            ControlPaint.DrawBorder(e.Graphics, recTab2, Color.Red, ButtonBorderStyle.Solid);
        }

Note: The blue rectangle will appear when you switch the focus to another tabPages.

Regards,

Frankie


这篇关于我几乎成功地在tabcontrol上画了一个tabitem边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆