在WinForms MDI应用程序中托管WPF用户控件时,如何避免出现视觉伪影? [英] How to avoid visual artifacts when hosting WPF user controls within a WinForms MDI app?

查看:115
本文介绍了在WinForms MDI应用程序中托管WPF用户控件时,如何避免出现视觉伪影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在WinForms MDI应用程序中托管WPF用户控件时,当您有多个相互重叠的表单时会出现图形问题,从而导致非常明显的视觉瑕疵.在将一个子窗体拖到另一个也承载WPF内容的子窗体之后,或者通过在拖动它时,主MDI父对象允许子窗体的边缘被剪切,这些伪像在大多数情况下是可见的.子窗体的拖放完成后,工件通常会停留在周围,但是我发现将焦点设置到其他应用程序的窗口上,然后重新聚焦到我的应用程序窗口上,重新绘制它,一切都很好,直到子窗体表单再次移动.请查看下面显示问题的图像.

When hosting WPF user controls within a WinForms MDI app there is a drawing issue when you have multiple forms that overlap each other that causes very distinct visual artifacts. These artifacts are mostly visible after dragging one child form over another one that also hosts WPF content or by allowing the edges of the child form to be clipped by the main MDI parent when dragging it around. After the drag and drop of the child form is completed the artifacts stay around generally but I've found that setting focus to a different application's window and then refocusing back on to my application window that it is redrawn and all is good again until the child forms are moved once again. Please see the image below which demonstrates the problem.

Microsoft的那些人坚持认为WinForms MDI已经是MDI的足够解决方案,并且不需要在WPF中进行重新发明,尽管我发现很难相信他们由于明显的缺点而尝试以这种方式创建WPF应用程序.

Those at Microsoft insist that the WinForms MDI is already a sufficient solution for MDI and doesn't need reinventing in WPF although I find it hard to believe they tried creating a WPF app this way because of the obvious shortcomings.

更新:我遗漏的一些其他注意事项是,如果我在不设置MdiParent的情况下创建这些表单,则会将它们创建为常规表单,并且不会发生此问题.对于WinForms MDI方案,此问题似乎是唯一的.另外,我目前正在Windows 7 Enterprise上运行,我知道在Windows XP上结果可能会完全不同,但是我无法对其进行测试.

UPDATE: A few extra notes that I left out is that if I create these Forms without setting the MdiParent they are created as regular forms and this issue doesn't happen. This issue seems unique to the WinForms MDI scenario. Also I've currently running on Windows 7 Enterprise and I'm aware the results may be quite different on Windows XP but I haven't been able to test this.

更新:我发现了一些我认为应该分享的与此问题相关的其他资源.

UPDATE: I've found a few other related resources on this issue that I thought I should share.

推荐答案

好吧,尽管感觉有点像黑客,但我可能已经找到了解决方案.看起来,如果在移动子MDI表单时在MDI父对象上调用Refresh方法,则说明的工件就会消失.在外观上,拖动窗口时看起来有些抖动,但似乎比我在原始帖子中显示的示例更容易接受.

Well, I may have found a solution although it feels like a bit of a hack. It appears that if you call the Refresh method on the MDI parent whenver a child MDI Form is moved that the noted artifacts go away. Visually things appear a bit jittery when dragging a window but it seems much more acceptable than the example I showed in my original post.

private void Form1_Move(object sender, EventArgs e)
{
    this.ParentForm.Refresh();

    System.Diagnostics.Debug.WriteLine(string.Format("Form Moved to: ({0},{1})", this.Left, this.Top));
}

我尝试了许多组合,例如仅刷新通过调​​用诸如 Update() Invalidate()之类的方法移动的子窗口. , Refresh(),此外,我还在MDI父级以及 Dispatcher.Invoke(DispatcherPriority.Render,...)上尝试了相同的方法我托管的WPF控件上的InvalidateVisual(),但其他任何方法都不能接受专门在MDI父级上调用 Refresh().

I've tried many combinations in the same vein such as refreshing just the child window that was being moved by calling methods such as Update(), Invalidate(), Refresh() and also I've tried these same methods on the MDI parent as well as Dispatcher.Invoke(DispatcherPriority.Render, ...) and InvalidateVisual() on my hosted WPF control but none of those other methods worked accept for calling Refresh() specifically on the MDI parent.

我意识到这可能不是最佳解决方案,因为我强迫子窗口每次移动几个像素时都要刷新整个主应用程序窗口,但是就目前而言,这是我发现可行的唯一合理的解决方案.如果其他人对此有任何替代解决方案或任何改进,我会很乐意接受您的回答.

I realize that this probably isn't the optimal solution since I'm forcing the whole main application window to refresh every time a child window moves a few pixels but as for right now it's the only reasonable solution that I found that works. If anybody else has any alternative solutions or any improvements upon this I will gladly accept your answer instead.

这篇关于在WinForms MDI应用程序中托管WPF用户控件时,如何避免出现视觉伪影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆