Windows 窗体 UserControl 覆盖不被调用
我正在创建一个从 UserControl 派生的 Windows 窗体控件,以嵌入到 WPF 应用程序中。 我通常遵循此链接中给出的过程。
public ref class CTiledImgViewControl : public UserControl
{
...
virtual void OnPaint( PaintEventArgs^ e ) override;
...
};
在我的 CPP 文件中:
void CTiledImgViewControl::OnPaint( PaintEventArgs^ e )
{
UserControl::OnPaint(e);
// do something interesting...
}
一切都会编译并运行,但是 OnPaint 方法永远不会被调用。
对要寻找的东西有什么想法吗? 我用 C++ 做了很多工作,但对 WinForms 和 WPF 还很陌生,所以这很可能是显而易见的事情......
I am creating a Windows Forms control derived from UserControl to be embedded in a WPF app. I have generally followed the procedures given in this link.
public ref class CTiledImgViewControl : public UserControl
{
...
virtual void OnPaint( PaintEventArgs^ e ) override;
...
};
And in my CPP file:
void CTiledImgViewControl::OnPaint( PaintEventArgs^ e )
{
UserControl::OnPaint(e);
// do something interesting...
}
Everything compiles and runs, however the OnPaint method is never getting called.
Any ideas of things to look for? I've done a lot with C++, but am pretty new to WinForms and WPF, so it could well be something obvious...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OnPaint
通常不会在UserControl
中调用,除非您在使用SetStyle
方法构造时设置了适当的样式。 您需要将UserPaint
样式设置为 true 才能调用OnPaint
。更新
我最近自己遇到了这个问题,并开始寻找答案。 我想在绘制期间执行一些计算(以利用绘制消息的独特处理),但我并不总是收到对
OnPaint
的调用。在深入研究 Reflector 后,我发现只有当相应的
WM_PAINT
消息的剪切矩形不为空时才会调用OnPaint
。 我的UserControl
实例有一个子控件,它填充了整个客户端区域,因此将其全部剪掉。 这意味着剪切矩形是空的,因此没有OnPaint
调用。我通过重写 WndProc 并直接添加 WM_PAINT 处理程序来解决这个问题,因为我找不到其他方法来实现我想要的效果。
The
OnPaint
won't normally get called in aUserControl
unless you set the appropriate style when it is constructed using theSetStyle
method. You need to set theUserPaint
style to true for theOnPaint
to get called.Update
I recently encountered this issue myself and went digging for an answer. I wanted to perform some calculations during a paint (to leverage the unique handling of paint messages) but I wasn't always getting a call to
OnPaint
.After digging around with Reflector, I discovered that
OnPaint
is only called if the clipping rectangle of the correspondingWM_PAINT
message is not empty. MyUserControl
instance had a child control that filled its entire client region and therefore, clipped it all. This meant that the clipping rectangle was empty and so noOnPaint
call.I worked around this by overriding
WndProc
and adding a handler forWM_PAINT
directly as I couldn't find another way to achieve what I wanted.我解决了这个问题,以防有人感兴趣。 这是因为我的 WinForms 控件嵌入在 ViewBox 中。 我将其更改为网格并立即开始获取绘制事件。 我想当询问有关 WPF 的问题时,您应该始终在问题中包含 XAML!
I solved the issue, in case anyone is interested. It was because my WinForms control was embedded in a ViewBox. I changed it to a grid and immediately started getting paint events. I guess when asking questions about WPF, you should always include the XAML in the question!