我在选项卡上旋转文本时遇到一些问题。选项卡最初工作得很好,但是我想在选中时加粗文本,所以我使用了Draw Item Event。我添加了一个RotateTransform和一个TranslateTransform,但它不起作用。该文本并没有显示出来。我遇到了麻烦,如果我将旋转移开,文本是可见的,但是当我使用旋转使文本垂直时,它会消失。这是我的代码:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
PointF tabPt = new PointF(_tabBounds.Left+(_tabBounds.Width), _tabBounds.Top+(_tabBounds.Height));
if (e.Index == tabControl1.SelectedIndex)
{
g.RotateTransform(-90);
g.TranslateTransform(tabPt.X, tabPt.Y);
g.DrawString(tabControl1.TabPages[e.Index].Text,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(tabPt.X, tabPt.Y));
g.ResetTransform();
}
else
{
g.TranslateTransform(tabPt.X, tabPt.Y);
g.RotateTransform(-90);
g.DrawString(tabControl1.TabPages[e.Index].Text,
tabControl1.Font,
_textBrush,
new PointF(tabPt.X,tabPt.Y));
g.ResetTransform();
}
}
}
非常感谢任何帮助。
修改 这是图像:
这是新代码:
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));
PointF tabPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width)/2);
if (e.Index == tabControl1.SelectedIndex)
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-(rotPt.X), -(rotPt.Y));
g.DrawString(tabText,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(rotPt.X, rotPt.Y));
}
else
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-rotPt.X, -rotPt.Y);
g.DrawString(tabText,
tabControl1.Font,
_textBrush,
new PointF(rotPt.X,rotPt.Y));
}
答案 0 :(得分:1)
感谢TaW的帮助。这是最终的代码及其工作原理。
public form1()
{
InitializeComponent();
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender,DrawItemEventArgs e)
{
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (textSize.Height / 2), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));
if (e.State.HasFlag(DrawItemState.Selected))
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-(rotPt.X), -(rotPt.Y));
g.DrawString(tabText,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(rotPt.X, rotPt.Y));
g.ResetTransform();
}
else
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-rotPt.X, -rotPt.Y);
g.DrawString(tabText,
tabControl1.Font,
_textBrush,
new PointF(rotPt.X,rotPt.Y));
g.ResetTransform();
}
}
P.S。我从来没有得过e.Bounds而不是GetTabRect(我不知道如何将它设置为选定的标签)。