摘要:[C#]TabControl 頁籤靠左 文字垂直顯示
先看成果
作法:
tabcontrol 的DrawMode先設為OwnerDrawFixed
tabcontrol加入DrawItem事件處理
private void tabcontrol_DrawItem(object sender, DrawItemEventArgs e)
{
StringFormat StrFormat = new StringFormat();
StrFormat.LineAlignment = StringAlignment.Center;// 設置文字垂直
StrFormat.Alignment = StringAlignment.Center;// 設置文字置中
// 填充色彩
SolidBrush bru = new SolidBrush(Color.FromArgb(72, 181, 250));
SolidBrush bruFont = new SolidBrush(Color.Black);// 字體色彩
Font font = new System.Drawing.Font("新細明體", 12,GraphicsUnit.Pixel);//字體樣式
for (int i = 0; i < tabChart.TabPages.Count; i++)
{
//獲取頁籤的工作區域
Rectangle recChild = tabChart.GetTabRect(i);
if (tabChart.SelectedIndex == i) //Selected顏色
{
e.Graphics.FillRectangle(new SolidBrush(Color.RoyalBlue), recChild);
bruFont = new SolidBrush(Color.White);
font = new System.Drawing.Font("新細明體", 12, GraphicsUnit.Pixel);
//繪制文字
e.Graphics.DrawString(tabChart.TabPages[i].Text, font, bruFont, recChild, StrFormat);
}
else //未Selected顏色
{
bruFont = new SolidBrush(Color.Black);
font = new System.Drawing.Font("新細明體", 12, GraphicsUnit.Pixel);
//繪制文字
e.Graphics.DrawString(tabChart.TabPages[i].Text, font, bruFont, recChild, StrFormat);
}
}
}