嘿,我一直在尝试绘制自己的TabControl以摆脱3D Shadow,但我没有取得多大成功。 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);
}
}
}
答案 0 :(得分:5)
我不确定这一点,但我相信如果你将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);
}
}
}
我认为这对你有用,但也可能有其他的做法。
答案 1 :(得分:1)
要启动DrawItem
事件,请在选项卡控件上设置DrawMode = OwnerDrawFixed
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.drawitem.aspx
答案 2 :(得分:0)
一种简单的方法是将TabControl的外观属性更改为按钮或 FlatButtons 并设置 DrawMode badk到正常。