如何阻止 System::Windows::Forms::UserControl 擦除其背景?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能正在寻找 Control.OnPaintBackground()< /a>. 我必须重写它,以便对我编写的将遗留 MFC 控件引入 Winforms 项目的自定义控件不执行任何操作。 否则,它将在 MFC 控件的绘制作业之上绘制背景。
本质上,在 .cpp 中:
在原型上:
通过事件参数传递给您什么矩形? 是整个控件失效还是其中一部分失效?
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:
On the prototype:
What rectangle is being passed in to you via the event args? Is the entire control being invalidated, or just a portion of it?
也许在 Form 的构造函数中是这样的语句:
我认为这根本阻止了
OnPaintBackground
被调用:所以你不需要重写OnPaintBackground
,而是可以擦除您自己在OnPaint
中设置背景(或不设置背景)。Maybe it's a statement like this in the Form's constructor:
I think that prevents
OnPaintBackground
being invoked at all: so you don't need to overrideOnPaintBackground
, and instead you can erase the background (or not) yourself in yourOnPaint
.我最近用 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.