windows.forms.panel 调用 onPaint 两次
我有一个面板,里面有多个面板。我已将主面板中的 OnPaint 重写为以下内容:
protected override void OnPaint(PaintEventArgs e)
{
Graphics graph = e.Graphics;
graph.Clear(Color.Black);
InvokePaintBackground(this, e);
graph.ScaleTransform(scale, scale);
foreach (childPanel child in childPanels)
{
child.onPaint(this, e);
}
graph.ResetTransform();
}
我遇到的问题是第一个控件(位置 0 中的控件)的 onPaint 函数被调用两次,因此我得到了两个版本的子面板,一个具有缩放功能,和一个没有。第二个 onPaint 似乎是由子控件本身调用的。
我怎样才能阻止它这样做?
I have a panel with multiple panels inside of it. I have overridden OnPaint in the master panel to the following:
protected override void OnPaint(PaintEventArgs e)
{
Graphics graph = e.Graphics;
graph.Clear(Color.Black);
InvokePaintBackground(this, e);
graph.ScaleTransform(scale, scale);
foreach (childPanel child in childPanels)
{
child.onPaint(this, e);
}
graph.ResetTransform();
}
The problem I have is that the onPaint function of the first control (control in spot 0) is being called twice so I am getting two versions of the child panel, one with scaling, and one without. The second onPaint seems to be called by the child control itself.
How do I keep it from doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为所有
Control
对象都进行自己的绘制,并且该方法由 Windows 自动调用。解决方案是根本不依赖此类功能 - 摆脱面板,或将Visible
设置为false
。That's because all
Control
objects do their own painting and the method is called automatically by Windows. The solution is to not rely on this sort of functionality at all - get rid of the panels, or setVisible
tofalse
.你到底为什么要在子控件上调用 OnPaint ? Windows 将自行管理绘制调用。您永远不应该直接调用它们,特别是对于从单独的绘制调用收到的图形上下文!
如果您需要请求绘制子控件,请使用 Invalidate方法代替。它将一个区域(或整个控件)标记为无效,以便 Windows 绘制它。这样做的好处是,Windows 足够聪明,知道如果您在同一个绘制周期中多次使其无效,它就不会多次重新绘制。
Why on earth are you calling OnPaint on the child control? Windows will manage the paint calls on its own. You should never call them directly, especially with the graphics context you received from a separate paint call!
If you need to request a child control to be painted, use the Invalidate method instead. It marks a region (or entire control) as invalid so that Windows will paint it. The upside to that is that Windows is smart enough to know that if you invalidate it multiple times in the same paint cycle, it won't re-draw multiple times.
这是固有的行为。您可以简单地使用
private bool secondaryCall;
变量,并仅在第二次调用时进行缩放。It is the inherent behavior. You can simply use a
private bool secondCall;
sort of a variable and do the scaling only on the second call.