我正在尝试创建一个自定义标签控件,它可以工作,但只有两件事我无法工作。
第一个问题; 我正在尝试在所选的选项卡之前添加一个填充的矩形,但是当我选择另一个选项卡时,它会保留在第一个选项卡中。
第二个问题; 这不是一个真正的问题,但可以改变这些tabitems的宽度吗?
这是代码;
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Bitmap b = new System.Drawing.Bitmap(Width, Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.Clear(System.Drawing.Color.FromArgb(246, 247, 249));
for (int i = 0; i <= TabCount - 1; i++)
{
System.Drawing.Rectangle tabRectangle = GetTabRect(i);
if (SelectedIndex == i)
{
//tab is selected
System.Drawing.SolidBrush lightBlue = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(239, 242, 247));
g.FillRectangle(lightBlue, tabRectangle);
// I can't get this working..
System.Drawing.SolidBrush sele = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(65, 95, 155));
g.FillRectangle(sele, new System.Drawing.Rectangle(0, 0, 5, 32));
}
else
{
//tab is not selected
g.FillRectangle(System.Drawing.Brushes.White, tabRectangle);
}
System.Drawing.SolidBrush fontColor = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(100, 99, 104));
g.DrawString(TabPages[i].Text, Font, fontColor, tabRectangle, new System.Drawing.StringFormat { Alignment = System.Drawing.StringAlignment.Near, LineAlignment = System.Drawing.StringAlignment.Center });
}
e.Graphics.DrawImage(b, 0, 0);
b.Dispose();
g.Dispose();
base.OnPaint(e);
}
感谢。
答案 0 :(得分:2)
您需要更改第二个矩形的位置,使其从与第一个矩形相同的位置开始:
//tab is selected
System.Drawing.SolidBrush lightBlue = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(239, 242, 247));
g.FillRectangle(lightBlue, tabRectangle);
// I can't get this working..
System.Drawing.SolidBrush sele = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(65, 95, 155));
g.FillRectangle(sele, new System.Drawing.Rectangle(
tabRectangle.X,tabRectangle.Y,
5, 32));
答案 1 :(得分:0)
您需要调用Invalidate()
强制控件重绘
这将重新触发OnPaint
来电。