调整窗口大小会导致黑条

Resizing window causes black strips(调整窗口大小会导致黑条)
本文介绍了调整窗口大小会导致黑条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,它在构造函数中设置这些样式:

I have a form, which sets these styles in constructor:

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

我在 Paint 事件中绘制了一些矩形.表单上没有控件.但是,当我调整表格大小时,表格的右侧和底部都有黑色条纹.有没有办法摆脱它们?我什么都试过了,在WndProc中监听WM_ERASEBKGND,在WM_PAINT上手动绘制表格,实现自定义双缓冲等等.有什么否则我可以试试吗?

And I draw some rectangles in Paint event. There are no controls on the form. Hovewer, when I resize the form, there are black strips at right and bottom of the form. Is there any way to get rid of them? I've tried everything, listening for WM_ERASEBKGND in WndProc, manually drawing the form on WM_PAINT, implementing custom double buffer, etc. Is there anything else I could try?

我发现了这个:https://connect.microsoft.com/VisualStudio/feedback/details/522441/custom-resizing-of-system-windows-window-flickers看起来这是 DWM 中的一个错误,但我只是希望我能做一些解决方法.

I've found this: https://connect.microsoft.com/VisualStudio/feedback/details/522441/custom-resizing-of-system-windows-window-flickers and it looks like it is a bug in DWM, but I just hope I can do some workaround.

请注意,我必须使用双缓冲,因为我想在 Paint 事件中绘制非常强烈的图形表示.我在 C# .NET 2.0、Win7 中开发.

Please note that I must use double buffering, since I want to draw pretty intense graphic presentation in the Paint event. I develop in C# .NET 2.0, Win7.

通过自己实现调整大小功能,我已经设法消除了大部分黑色条纹.但是,仍然存在一些小故障.有没有办法同时进行 resizepaint 操作?这是我需要做的伪代码:

I've managed to get rid of most of the black stripes by implementing the resize functionality by myself. Hovewer there are still some minor glitches. Is there any way to do resize and paint operation at once? Here is a pseudo-code of what I need to do:

IntPtr hDC;
var size = new Size(250, 200);
IntPtr handle = API.PaintAndResizeBegin(this.Handle /* Form.Handle */,
                                        size.Width, size.Height, out hDC);
using (var g = Graphics.FromHdc(hDC)) {
    this.backBuffer.Render(g, size);
}
API.PaintAndResizeCommit(handle);

有没有办法实现上面的代码?

Is there any way to implement the above code?

第二种解决方案可能是回缓冲整个表单,包括非客户区.但是怎么做呢?我不想自己绘制非客户区域,因为我想在 Vista/7 上保持良好的航空效果.任何帮助将不胜感激.

The second solution could be to back-buffer whole form, including non-client area. But how to do that? I don't want to paint the non-client area by myself, as I want to keep the nice aero effect on Vista/7. Any help will be deeply appreciated.

看起来这个问题是无法解决的,因为它在 Windows 上无处不在,在每个应用程序中.我们只能希望 MS 在 Mac OS X 中获得一些灵感,并在新的 Windows 中提供适当的 API.

It looks like this problem is unsolvable, since it is omnipresent on Windows, in every application. We can just hope that MS will take some inspiration in Mac OS X and will provide appropriate APIs in new Windows.

推荐答案

我找到了可以同时绘制和调整窗口大小的功能 - UpdateLayeredWindow.

I've found the function which can paint and resize window at the same time - UpdateLayeredWindow.

所以现在应该可以创建可调整大小的窗口,这些窗口在调整大小时没有任何条带.但是,您需要自己绘制窗口内容,因此有点不方便.但是我认为使用WPFUpdateLayeredWindow应该没有问题.

So now it should be possible to create resizable windows, which do not have any strips while being resized. However, you need to paint the window content yourself, so it is a little inconvenient. But I think that using WPF and UpdateLayeredWindow, there shouldn't be any problem.

发现问题.:-) 当使用 UpdateLayeredWindow 时,您必须自己绘制窗口的边框.所以,如果你想在 win7 中使用 UpdateLayeredWindow 绘制具有漂亮玻璃效果的标准窗口,那你就完蛋了.

Found problems. :-) When using UpdateLayeredWindow, you must paint the window's border yourself. So, if you want standard window painted using UpdateLayeredWindow with nice glass effect in win7, you are screwed.

在 Microsft Connect 甚至是一个关于这个问题的线程,微软说这是一个设计上的错误,如果它得到修复,那么可能在 Win8 或一些更新的系统中.所以我们对此无能为力.

On Microsft Connect is even a thread about this problem, where Microsoft says it is a bug by design, and if it ever gets fixed, then probably in Win8 or some newer system. So there isn't much we could do about this.

这篇关于调整窗口大小会导致黑条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)