首先设置DrawMode 属性
tabControl.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += new DrawItemEventHandler(this.tabControl_DrawItem);
然后tabControl_DrawItem事件中代码如下:
Brush bshBack, bshFore;
Rectangle fillRect;
e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), this.tabControl.ClientRectangle);
//画样式
for (int i = 0; i < tabControl.TabPages.Count; i++)
{
fillRect = tabControl.GetTabRect(i);
Rectangle recTab = new Rectangle(fillRect.X, fillRect.Y, fillRect.Width – 1, fillRect.Height);
string tabName = this.tabControl.TabPages[i].Text;
StringFormat sftTab = new StringFormat();
if (this.tabControl.SelectedIndex == i)//当前Tab页的样式
{
bshBack = new SolidBrush(this.TabControlSelectedBackColor);
bshFore = new SolidBrush(this.TabControlSelectedForeColor);
}
else//其余Tab页的样式
{
bshBack = new SolidBrush(this.TabControlDefaultBackColor);
bshFore = new SolidBrush(this.TabControlDefaultForeColor);
}
e.Graphics.FillRectangle(bshBack, recTab);
e.Graphics.DrawString(tabName, e.Font, bshFore, recTab, sftTab);
this.tabControl.TabPages[i].BackColor = this.TabPageBackColor;
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明自定义TabControl 分页标题样式!