每个子控件都会调用 OnPaint 方法
我有一个 UserControl(WinForms,.net 2.0),并且我有这个:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var rect = e.ClipRectangle;
var pen = new Pen(Brushes.LightGray, 1);
e.Graphics.DrawRectangle(pen, rect);
}
我基本上想在 UserControl 上绘制边框,但也在所有子控件中绘制矩形! 我从来没有读过应该为每个子控件调用它,有解决方案吗?
I've a UserControl (WinForms, .net 2.0), and I've this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var rect = e.ClipRectangle;
var pen = new Pen(Brushes.LightGray, 1);
e.Graphics.DrawRectangle(pen, rect);
}
I basically want to draw a border on the UserControl, but the rectangle is being draw in all the child controls too!
I never read it should be called for every child control, is there a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么使用 PaintEventArgs.ClipRectangle 来确定矩形的边界? 尝试使用
Control.ClientRectangle
相反。Why are you using
PaintEventArgs.ClipRectangle
to determine the bounds of the rectangle? Try usingControl.ClientRectangle
instead.因为子控件使父控件无效,导致每个子控件都会触发此方法。
不要使用 e 作为参数(e 将是触发事件的任何控件,无论是否是子控件),而是显式使用控件名称。
Because the child controls invalidate the parent, causing this method to be fired for each one.
Instead of using e as the parameter (e is going to be whichever control fired the event, child or not) use the control name explicity.