如何阻止 System::Windows::Forms::UserControl 擦除其背景?

发布于 2024-07-27 03:35:20 字数 690 浏览 7 评论 0原文

我有一个 C++/CLI System::Windows::Forms::UserControl 派生控件,该控件只应在将新数据输入其中时重绘(一小部分)自身。 但出于某种原因,即使应用程序外部没有任何原因,OnPaint 机制也会被调用。

这是一个片段:

void Spectrogram::OnPaint(System::Windows::Forms::PaintEventArgs ^e)
{
    // Overidden to stop the background being painted(?)
}

void Spectrogram::AddNewFFTData( float* data, int fataWidth )
{
    Graphics^ gfx = CreateGraphics();

    //now do some drawing

    gfx->Dispose();
}

因此,我调用添加数据方法来添加一些新数据,理论上这些新数据应该覆盖前一个条目(这会清除一些突出显示)并写入新条目。

以前,我曾经开发 MFC/OpenGL 应用程序,我要做的第一件事就是重写 OnEraseBackground 方法。 但据我所知,没有明显的方法可以阻止它被删除。 我错过了什么?

I have a C++/CLI System::Windows::Forms::UserControl derived control which should only redraw (a small portion of) itself as new data is fed into it. For some reason though, the OnPaint mechanism is being called even when there's nothing to cause it external to the app.

Here's a snippet:

void Spectrogram::OnPaint(System::Windows::Forms::PaintEventArgs ^e)
{
    // Overidden to stop the background being painted(?)
}

void Spectrogram::AddNewFFTData( float* data, int fataWidth )
{
    Graphics^ gfx = CreateGraphics();

    //now do some drawing

    gfx->Dispose();
}

So I call the add data method to add some new data which should in theory write over the previous entry (which clears some highlighting) and write the new entry.

Back in the day I used to develop MFC/OpenGL apps and one of the first things I'd do would be to override the OnEraseBackground method. As far as I can see though, there's no obvious way of stopping it being erased. What have I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

路弥 2024-08-03 03:35:21

您可能正在寻找 Control.OnPaintBackground()< /a>. 我必须重写它,以便对我编写的将遗留 MFC 控件引入 Winforms 项目的自定义控件不执行任何操作。 否则,它将在 MFC 控件的绘制作业之上绘制背景。

本质上,在 .cpp 中:

void MyControl::OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent)
{
    // Do nothing, we don't want to paint over the native control.
    // You may want to do something a little fancier for DesignMode
    // if you use the winforms designer, though.
}

在原型上:

protected: 
    void virtual OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent) override;

通过事件参数传递给您什么矩形? 是整个控件失效还是其中一部分失效?

You may be looking for Control.OnPaintBackground(). I've had to override that to do nothing for a custom control I wrote to bring a legacy MFC control into a Winforms project. Otherwise it would paint the background on top of the MFC control's paint job.

Essentially, in the .cpp:

void MyControl::OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent)
{
    // Do nothing, we don't want to paint over the native control.
    // You may want to do something a little fancier for DesignMode
    // if you use the winforms designer, though.
}

On the prototype:

protected: 
    void virtual OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent) override;

What rectangle is being passed in to you via the event args? Is the entire control being invalidated, or just a portion of it?

栀梦 2024-08-03 03:35:21

也许在 Form 的构造函数中是这样的语句:

//do own background painting
this.SetStyle(ControlStyles.Opaque, true);

我认为这根本阻止了 OnPaintBackground 被调用:所以你不需要重写 OnPaintBackground,而是可以擦除您自己在 OnPaint 中设置背景(或不设置背景)。

Maybe it's a statement like this in the Form's constructor:

//do own background painting
this.SetStyle(ControlStyles.Opaque, true);

I think that prevents OnPaintBackground being invoked at all: so you don't need to override OnPaintBackground, and instead you can erase the background (or not) yourself in your OnPaint.

我不吻晚风 2024-08-03 03:35:21

我最近用 OnPaint 做了一些东西(C#,如果这很重要的话),我注意到当控件的某个区域被显示时,它实际上是被绘制的。

更好的解决方案是在缓存的位图上绘制,并在每次 dotNet 请求时将其绘制到控件。

I did some stuff with the OnPaint lately (C#, if that matters), and I noticed it literally is drawn when a area of the control is revealed.

A better solution is to draw on a cached Bitmap, and draw it to the control every time dotNet asks for it.

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