Windows 窗体 UserControl 覆盖不被调用

发布于 2024-07-07 11:31:12 字数 631 浏览 9 评论 0原文

我正在创建一个从 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 技术交流群。

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

发布评论

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

评论(2

爱人如己 2024-07-14 11:31:12

OnPaint 通常不会在 UserControl 中调用,除非您在使用 SetStyle 方法构造时设置了适当的样式。 您需要将 UserPaint 样式设置为 true 才能调用 OnPaint

SetStyle(ControlStyles::UserPaint, true);

更新

我最近自己遇到了这个问题,并开始寻找答案。 我想在绘制期间执行一些计算(以利用绘制消息的独特处理),但我并不总是收到对 OnPaint 的调用。

在深入研究 Reflector 后,我发现只有当相应的 WM_PAINT 消息的剪切矩形不为空时才会调用 OnPaint 。 我的 UserControl 实例有一个子控件,它填充了整个客户端区域,因此将其全部剪掉。 这意味着剪切矩形是空的,因此没有 OnPaint 调用。

我通过重写 WndProc 并直接添加 WM_PAINT 处理程序来解决这个问题,因为我找不到其他方法来实现我想要的效果。

The OnPaint won't normally get called in a UserControl unless you set the appropriate style when it is constructed using the SetStyle method. You need to set the UserPaint style to true for the OnPaint to get called.

SetStyle(ControlStyles::UserPaint, true);

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 corresponding WM_PAINT message is not empty. My UserControl 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 no OnPaint call.

I worked around this by overriding WndProc and adding a handler for WM_PAINT directly as I couldn't find another way to achieve what I wanted.

薔薇婲 2024-07-14 11:31:12

我解决了这个问题,以防有人感兴趣。 这是因为我的 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!

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