效果图:

代码:
1.设置控件属性
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.Alignment = TabAlignment.Left;
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.Multiline = true;
tabControl1.ItemSize = new Size(50, 100);
2.设置事件
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle tabArea = tabControl1.GetTabRect(e.Index);//主要是做个转换来获得TAB项的RECTANGELF
RectangleF tabTextArea = (RectangleF)(tabControl1.GetTabRect(e.Index));
Graphics g = e.Graphics;
StringFormat sf = new StringFormat();//封装文本布局信息
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Font font = this.tabControl1.Font;
SolidBrush brush = new SolidBrush(Color.Black);//绘制边框的画笔
g.DrawString(((TabControl)(sender)).TabPages[e.Index].Text, font, brush, tabTextArea, sf);
}
本文介绍如何使用C#自定义Windows Forms中TabControl的样式,包括设置控件属性和重写DrawItem事件,实现垂直标签和自定义外观。
3万+

被折叠的 条评论
为什么被折叠?



