gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:15:04 24 4
gpt4 key购买 nike

我已经考虑了几天,但我认为我对 windows 和 wpf 在内部如何工作以解决这个问题缺乏一些基本的了解。

问题是这样的:

我创建了一个窗口,可以让我在 aero 标题栏(如 office)上绘制 wpf 控件。
只要我不向窗口添加 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 ) { }
}
}

最佳答案

好吧,我终于找到了一个修复方法,但我认为这个修复方法接近于黑魔法……无论如何,事实证明,使用任何小于窗口的矩形来响应 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;

我不知道为什么这行得通,我确信存在一个更明智的解决方案,所以如果有人能启发我,我会很高兴。

关于wpf - 如果窗口包含 HwndHost 元素,则自定义 dwm 绘制的窗口框架会在调整大小时闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com