the*_*st_ 3 c# tabcontrol winforms
嘿,我一直在尝试绘制自己的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我不确定这一点,但我相信如果你将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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这对你有用,但也可能有其他方法.
小智 6
为了触发DrawItem事件,请DrawMode = OwnerDrawFixed在选项卡控件上
设置http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.drawitem.aspx