概述Windows Form中的TabControl重绘http://blog.canself.com/tag/tabcontrol%E9%87%8D%E7%BB%98/最近整理自己工作以来的旧项目,将其中的一些技术点整理出来。这个tabcontrol重绘相关的项目是自己毕业不久就做的一个,针对这个项目后面还有wpf版,暂且不提,暂时先看这个。下面是软件的主界面 Windows Form中的TabControl重绘 http://blog.canself.com/tag/tabcontrol%E9%87%8D%E7%BB%98/
最近整理自己工作以来的旧项目,将其中的一些技术点整理出来。这个tabcontrol重绘相关的项目是自己毕业不久就做的一个,针对这个项目后面还有wpf版,暂且不提,暂时先看这个。
下面是软件的主界面,当然这个重绘的还不够完全,针对tabcontrol边框的处理还不好。
可以看出,也有一定效果的。
下面说说步骤,看看代码:
1、首先就是设置tabcontrol的drawmode属性,使其重绘有效。
1 | this .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)
3 | Graphics g = e.Graphics; |
4
| 5 | TabPage changedpage = tabc_draw.TabPages[e.Index];//当前处理标签 |
6
Rectangle backrect = tabc_draw.GetTabRect(e.Index);//标签背景区域 | 8
Brush Fontbrush;//标签字体颜色 | 10
Pen borderpen;//边框颜色 | 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); | 24
else | 26
SolIDBrush(color.White); | 28
"楷体"29 | Pen(color.DarkGreen); | 30
| } | 32
g.FillRectangle(backbrush,backrect); | 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(); | 48
Fontbrush.dispose(); | 50
} | 注释都在代码里了,就不详述了。下面是最后的效果的【只是简单的实现,自己多写写,模仿下就可以写出好效果啦】
总结
以上是内存溢出为你收集整理的Windows Form中的TabControl重绘全部内容,希望文章能够帮你解决Windows Form中的TabControl重绘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)