如果窗口包含HwndHost元素,则自定义的dwm绘制窗口框架在调整大小时会闪烁

9

我已经思考了几天,但我认为我缺乏一些关于Windows和WPF内部工作的基本理解来解决这个问题。

问题在于:

我创建了一个窗口,应该让我在Aero标题栏上绘制wpf控件(就像office一样)。只要我不向窗口添加Hwndhost元素,它就可以正常工作。但是,一旦我添加了Hwndhost元素,每当我调整大小时,框架和HwndHost就会开始闪烁得非常厉害(其他元素似乎可以正确呈现)。我还尝试使用来自WPF Shell Integration library的自定义框架窗口实现,结果是相同的,所以我认为这不完全是我的错。

以下代码是一个简单的可编译程序,可重现此问题。示例使用c#编写,但答案不必如此。

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;

namespace DwmTest {
    class Program {
        [STAThread]
        static void Main( ) {
            var w = new CustomFrameWindow{ Content =  new WindowHost() };
            w.Show( );
            ((Border)VisualTreeHelper.GetChild( w, 0 )).Margin = new Thickness( 11, 33, 11, 11 );
            Dispatcher.Run( );
        }
    }

    public class CustomFrameWindow : Window {

        const int resizeFrameWidth = 11;
        const int captionHeight = 33;

        public enum HT { CLIENT = 1, CAPTION = 2, LEFT = 10, RIGHT, TOP, TOPLEFT, TOPRIGHT, BOTTOM, BOTTOMLEFT, BOTTOMRIGHT }

        [StructLayout( LayoutKind.Sequential )]
        public struct Margins { public int left, right, top, bottom; }

        [DllImport( "user32.dll" )]
        public static extern bool SetWindowPos( IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags );

        [DllImport( "dwmapi.dll" )]
        public static extern bool DwmDefWindowProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, out IntPtr result );

        [DllImport( "dwmapi.dll", PreserveSig = false )]
        public static extern void DwmExtendFrameIntoClientArea( IntPtr hwnd, ref Margins pMarInset );

        protected override void OnSourceInitialized( EventArgs e ) {
            base.OnSourceInitialized( e );

            var hWndSource = HwndSource.FromHwnd( new WindowInteropHelper( this ).Handle );
            hWndSource.CompositionTarget.BackgroundColor = Colors.Transparent;

            var nonClientArea = new Margins{ 
                left = resizeFrameWidth, top = captionHeight, bottom = resizeFrameWidth, right = resizeFrameWidth
            };
            DwmExtendFrameIntoClientArea( hWndSource.Handle, ref nonClientArea );

            hWndSource.AddHook( WndProc );

            // FRAMECHANGED | NOMOVE | NOSIZE
            SetWindowPos( hWndSource.Handle, new IntPtr( ), 0, 0, 0, 0, 0x0020 | 0x0002 | 0x0001 );
        }

        private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) {

            switch( msg ) {
                case 0x0083: // NCCALCSIZE
                    if( wParam != IntPtr.Zero ) handled = true;
                    break;
                case 0x0084: // NCHITTEST
                    handled = true;

                    IntPtr dwmHitTest;
                    if( DwmDefWindowProc( hwnd, msg, wParam, lParam, out dwmHitTest ) ) {
                        return dwmHitTest;
                    }

                    var mousePosition = PointFromScreen( new Point( lParam.ToInt32( ) & 0xFFFF, lParam.ToInt32( ) >> 16 ) );

                    var isTop = mousePosition.Y <= resizeFrameWidth;
                    var isBottom = mousePosition.Y >= ActualHeight - resizeFrameWidth;
                    var isLeft = mousePosition.X <= resizeFrameWidth;
                    var isRight = mousePosition.X >= ActualWidth - resizeFrameWidth;

                    var hitTest = HT.CLIENT;
                    if( isTop ) {
                        if( isLeft ) hitTest = HT.TOPLEFT;
                        else if( isRight ) hitTest = HT.TOPRIGHT;
                        else hitTest = HT.TOP;
                    }
                    else if( isBottom ) {
                        if( isLeft ) hitTest = HT.BOTTOMLEFT;
                        else if( isRight ) hitTest = HT.BOTTOMRIGHT;
                        else hitTest = HT.BOTTOM;
                    }
                    else if( isLeft ) hitTest = HT.LEFT; 
                    else if( isRight ) hitTest = HT.RIGHT; 
                    else if( mousePosition.Y <= captionHeight ) hitTest = HT.CAPTION;

                    return new IntPtr( (int)hitTest );
            }
            return IntPtr.Zero;
        }
    }

    public class WindowHost : HwndHost {
        [DllImport( "user32.dll", SetLastError = true )]
        static extern IntPtr CreateWindowEx( IntPtr exStyle, string lpClassName,string lpWindowName,int dwStyle,int x,int y,int nWidth,int nHeight,IntPtr hWndParent,IntPtr hMenu,IntPtr hInstance,IntPtr lpParam );

        protected override HandleRef BuildWindowCore( HandleRef hWndParent ) {
            return new HandleRef( this, CreateWindowEx( IntPtr.Zero, "static", "", 0x40000000, 0, 0, 200, 200, hWndParent.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ) );
        }
        protected override void DestroyWindowCore( HandleRef hwnd ) { }
    }
}
2个回答

5

好的,我终于找到了一个解决方法,但我认为这个方法有点像黑魔法...无论如何,事实证明,响应WM_NCCALCSIZE并使用任何小于窗口的矩形都可以解决问题。 例如,将处理程序更改为以下内容即可消除闪烁!

            case WM.NCCALCSIZE:
                if( wParam != IntPtr.Zero ) {
                    handled = true;
                    var client = (RECT)Marshal.PtrToStructure( lParam, typeof( RECT ) );
                    client.Bottom -= 1;
                    Marshal.StructureToPtr( client, lParam, false );
                }
                break;

我不知道为什么这个能够工作,而且我相信一定有更合理的解决方案,所以如果有人能够启示我,我会很高兴。


你有没有找到任何替代方案? - Seth
@Seth 不过说实话,我在找到这个后就没有再看了...如果你发现了什么,请告诉我! - Roald
@Roald,这个在之前的Windows版本中运行得很好,但是在目前的Windows 10 Build 1903中,窗口又开始闪烁了。虽然与其他窗口相比,它的闪烁现象较少,但是否有任何方法可以完全解决这个问题呢? - trickymind
如果您有解决方案,那将非常有帮助。 - trickymind

4

我通过在CreatWindowEx中添加WS_CLIPCHILDREN样式来解决这个问题。

protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
    _hwndHost = Win32Api.CreateWindowEx(0, "Static", "", 
                        (int) (WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN),
                      0, 0,
                      _hostWidth, _hostHeight,
                      hwndParent.Handle,
                      IntPtr.Zero,
                      IntPtr.Zero,
                      0);

 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接