windows.forms.panel 调用 onPaint 两次

发布于 2024-11-07 01:28:15 字数 561 浏览 0 评论 0原文

我有一个面板,里面有多个面板。我已将主面板中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未蓝澄海的烟 2024-11-14 01:28:15

这是因为所有 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 set Visible to false.

聆听风音 2024-11-14 01:28:15

你到底为什么要在子控件上调用 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.

人心善变 2024-11-14 01:28:15

这是固有的行为。您可以简单地使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文