TabControl.DrawItem 未在用户绘制的 TabControl 上触发
嘿,我一直在尝试绘制自己的 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);
}
}
}
Hey, I've been trying to paint my own TabControl to get rid of the 3D Shadow but I am not having much success. The DrawItem event isn't firing at the moment. Do I have to shoot it myself? How do I do that?
Code:
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此不确定,但我相信如果您将 ControlStyles.UserPaint 位指定为 true,则 DrawItem 将不会触发。不过,其他 ControlStyle(AllPaintingInWmPaint 和 DoubleBuffer)相互依赖,因此您也需要将它们删除。但是,不将 UserPaint 位设置为 true 将导致 Paint 事件不会被触发。我一直在做的是重写 OnPaintBackground 方法:
我认为这对您有用,但也可能有其他方法可以做到这一点。
I'm not sure on this, but I believe if you specify the ControlStyles.UserPaint bit to true, then the DrawItem won't fire. The other ControlStyles (AllPaintingInWmPaint and DoubleBuffer) have dependencies on each other, though, so you would need to leave them off as well. However, not setting the UserPaint bit to true will result in the Paint event not getting fired. What I have been doing is overriding the OnPaintBackground method:
I think this will work for you, but there may be other methods of doing it as well.
为了触发
DrawItem
事件,请在选项卡控件上设置DrawMode = OwnerDrawFixed
http://msdn.microsoft.com/en -us/library/system.windows.forms.tabcontrol.drawitem.aspx
In order to get
DrawItem
event fired, setDrawMode = OwnerDrawFixed
on the Tab Controlhttp://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.drawitem.aspx
一种简单的方法是将 TabControl 的 Appearance 属性更改为 Buttons 或 FlatButtons 并将 DrawMode badk 设置为 <强>正常。
One simple way is to change the TabControl's Appearance property to either Buttons or FlatButtons and set the DrawMode badk to Normal.