加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Windows Form中的TabControl重绘

发布时间:2021-07-06 05:55:45 所属栏目:大数据 来源: https://blog.csdn.net/kasama1
导读:Windows Form中的TabControl重绘 http://blog.canself.com/tag/tabcontrol%E9%87%8D%E7%BB%98/ 最近整理自己工作以来的旧项目,将其中的一些技术点整理出来。这个tabcontrol重绘相关的项目是自己毕业不久就做的一个,针对这个项目后面还有wpf版,暂且不提,
1this.tabc_draw.DrawMode = TabDrawMode.OwnerDrawFixed;

2、写tabcontrol的drawitem事件。

? ? ? ? ? ?Graphics g = e.Graphics;


? ? ? ? ? ? TabPage changedpage = tabc_draw.TabPages[e.Index];//当前处理标签
? ? ? ? ? ? Rectangle backrect = tabc_draw.GetTabRect(e.Index);//标签背景区域
? ? ? ? ? ? Brush backbrush;//标签背景色
? ? ? ? ? ? Brush fontbrush;//标签字体颜色
? ? ? ? ? ? Font tabFont;//标签字体
? ? ? ? ? ? Pen borderpen;//边框颜色


? ? ? ? ? ? //TabControl绘制
? ? ? ? ? ? Brush backtabcontrol = new SolidBrush(Color.Transparent);
? ? ? ? ? ? g.FillRectangle(backtabcontrol,this.tabc_draw.ClientRectangle.X + 100,this.tabc_draw.ClientRectangle.Y,this.tabc_draw.ClientRectangle.Size.Width,this.tabc_draw.ItemSize.Height);
? ? ? ? ? ? backtabcontrol.Dispose();


? ? ? ? ? ? if (e.State == DrawItemState.Selected)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? backbrush = new SolidBrush(Color.White);
? ? ? ? ? ? ? ? fontbrush = new SolidBrush(Color.Blue);
? ? ? ? ? ? ? ? tabFont = new Font("宋体",14,(FontStyle.Bold|FontStyle.Italic),GraphicsUnit.Pixel);
? ? ? ? ? ? ? ? borderpen = new Pen(Color.LightBlue);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? backbrush = new SolidBrush(Color.WhiteSmoke);
? ? ? ? ? ? ? ? fontbrush = new SolidBrush(Color.Black);
? ? ? ? ? ? ? ? tabFont = new Font("宋体",12,FontStyle.Regular,GraphicsUnit.Pixel);
? ? ? ? ? ? ? ? borderpen = new Pen(Color.DarkGreen);
? ? ? ? ? ? }
? ? ? ? ? ? //绘制标签背景
? ? ? ? ? ? g.FillRectangle(backbrush,backrect);


? ? ? ? ? ? //绘制标签字体
? ? ? ? ? ? StringFormat _StringFlags = new StringFormat();
? ? ? ? ? ? _StringFlags.Alignment = StringAlignment.Center;
? ? ? ? ? ? _StringFlags.LineAlignment = StringAlignment.Center;
? ? ? ? ? ? g.DrawString(changedpage.Text,tabFont,fontbrush,backrect,new StringFormat(_StringFlags));
? ? ? ? ? ? //绘制非标签原始名称【可依据e.State修改】 g.DrawString("呵呵",new StringFormat(_StringFlags));


? ? ? ? ? ? //绘制标签边框
? ? ? ? ? ? //backrect.Offset(1,1);
? ? ? ? ? ? //backrect.Inflate(2,2);
? ? ? ? ? ? //g.DrawRectangle(borderpen,backrect);


? ? ? ? ? ? backbrush.Dispose();
? ? ? ? ? ? tabFont.Dispose();
? ? ? ? ? ? fontbrush.Dispose();
? ? ? ? ? ? borderpen.Dispose();

void?tabc_draw_DrawItem(object?sender,DrawItemEventArgs e) 2{ 3????Graphics g = e.Graphics; 4 ? 5 TabPage changedpage = tabc_draw.TabPages[e.Index];//当前处理标签 6 Rectangle backrect = tabc_draw.GetTabRect(e.Index);//标签背景区域 7 Brush backbrush;//标签背景色 8 Brush fontbrush;//标签字体颜色 9 Font tabFont;//标签字体 10 Pen borderpen;//边框颜色 11 ? 12 ????//TabControl绘制 13 Brush backtabcontrol =?new?SolidBrush(Color.Black); 14 g.FillRectangle(backtabcontrol,.tabc_draw.ClientRectangle.X + 100,monospace!important; font-size:1em!important; margin:0px!important; outline:0px!important; padding:0px!important; vertical-align:baseline!important; float:none!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">.tabc_draw.ClientRectangle.Y,monospace!important; font-size:1em!important; margin:0px!important; outline:0px!important; padding:0px!important; vertical-align:baseline!important; float:none!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">.tabc_draw.ClientRectangle.Size.Width,monospace!important; font-size:1em!important; margin:0px!important; outline:0px!important; padding:0px!important; vertical-align:baseline!important; float:none!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">.tabc_draw.ItemSize.Height); 15 backtabcontrol.Dispose(); 16 ? 17 ????if?(e.State == DrawItemState.Selected) 18 19 ????????backbrush =?20 fontbrush =?SolidBrush(Color.Yellow); 21 tabFont =?Font("宋体",15,FontStyle.Bold,GraphicsUnit.Pixel); 22 borderpen =?Pen(Color.LightBlue); 23 } 24 else 25 { 26 SolidBrush(Color.White); 27 SolidBrush(Color.Red); 28 "楷体" 29 Pen(Color.DarkGreen); 30 } 31 //绘制标签背景 32 g.FillRectangle(backbrush,backrect); 33 34 //绘制标签字体 35 StringFormat _StringFlags =?StringFormat(); 36 _StringFlags.Alignment = StringAlignment.Center; 37 _StringFlags.LineAlignment = StringAlignment.Center; 38 g.DrawString(changedpage.Text,?StringFormat(_StringFlags)); 39 //绘制非标签原始名称【可依据e.State修改】 g.DrawString("呵呵",new StringFormat(_StringFlags)); 40 41 //绘制标签边框 42 //backrect.Offset(1,1); 43 //backrect.Inflate(2,2); 44 g.DrawRectangle(borderpen,monospace!important; font-size:1em!important; margin:0px!important; outline:0px!important; padding:0px 0.3em 0px 0px!important; vertical-align:baseline!important; text-align:right!important; float:none!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-height:1.1em!important; direction:ltr!important; display:block!important; background:none!important">45 46 backbrush.Dispose(); 47 tabFont.Dispose(); 48 fontbrush.Dispose(); 49 borderpen.Dispose(); 50 }

注释都在代码里了,就不详述了。下面是最后的效果的【只是简单的实现,自己多写写,模仿下就可以写出好效果啦】

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

Windows Form中的TabControl重绘

http://blog.canself.com/tag/tabcontrol%E9%87%8D%E7%BB%98/

最近整理自己工作以来的旧项目,将其中的一些技术点整理出来。这个tabcontrol重绘相关的项目是自己毕业不久就做的一个,针对这个项目后面还有wpf版,暂且不提,暂时先看这个。

下面是软件的主界面,当然这个重绘的还不够完全,针对tabcontrol边框的处理还不好。

可以看出,也有一定效果的。

下面说说步骤,看看代码:

1、首先就是设置tabcontrol的drawmode属性,使其重绘有效。

    推荐文章
      热点阅读