嘿,我一直在尝试绘制自己的 TabControl 以摆脱 3D 阴影,但我没有取得太大的成功。 DrawItem 事件目前未触发。必须自己拍吗?我该怎么做?
代码:
namespace NCPad
{
public partial class NCE_TabControl : TabControl
{
Rectangle TabBoundary;
RectangleF TabTextBoundary;
public NCE_TabControl()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.Paint += new PaintEventHandler(this.OnPaint);
this.DrawItem += new DrawItemEventHandler(this.OnDrawItem);
}
protected void OnPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), e.ClipRectangle);
}
protected void OnDrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Blue), this.TabBoundary);
MessageBox.Show("hi");
}
protected override void OnLayout(LayoutEventArgs levent)
{
base.OnLayout(levent);
this.TabBoundary = this.GetTabRect(0);
this.TabTextBoundary = (RectangleF)this.GetTabRect(0);
}
}
}
最佳答案
对此我不确定,但我相信如果您将 ControlStyles.UserPaint 位指定为 true,则 DrawItem 不会触发。不过,其他 ControlStyles(AllPaintingInWmPaint 和 DoubleBuffer)相互依赖,因此您也需要将它们关闭。但是,不将 UserPaint 位设置为 true 将导致不触发 Paint 事件。我一直在做的是覆盖 OnPaintBackground 方法:
public partial class NCE_TabControl : TabControl
{
Rectangle TabBoundary;
RectangleF TabTextBoundary;
StringFormat format = new StringFormat(); //for tab header text
public NCE_TabControl()
{ InitializeComponent();
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.format.Alignment = StringAlignment.Center;
this.format.LineAlignment = StringAlignment.Center;
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), 0, 0, this.Size.Width, this.Size.Height);
foreach (TabPage tp in this.TabPages)
{
//drawItem
int index = this.TabPages.IndexOf(tp);
this.TabBoundary = this.GetTabRect(index);
this.TabTextBoundary = (RectangleF)this.GetTabRect(index);
g.FillRectangle(new SolidBrush(Color.LightBlue), this.TabBoundary);
g.DrawString("tabPage " + index.ToString(), this.Font, new SolidBrush(Color.Black), this.TabTextBoundary, format);
}
}
}
我认为这对您有用,但也可能有其他方法。
关于c# - TabControl.DrawItem 未在用户绘制的 TabControl 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115321/