简体   繁体   English

分层(透明)WPF 窗口中的 Win32 本机控件不可见

[英]Win32 native control in layered (transparent) WPF window is invisible

I want to host a native Win32 (Qt widget) control in a WPF window.我想在 WPF 窗口中托管本机 Win32(Qt 小部件)控件。 My problem is that in case I host it in a normal WPF window, everything works fine, but when I set AllowsTransparency of the WPF window to true , the native content won't be rendered anymore.我的问题是,如果我在普通 WPF 窗口中托管它,一切正常,但是当我将 WPF 窗口的AllowsTransparency设置为true ,将不再呈现本机内容。 I've made a simple test application which creates only a Win32 button to see if Qt is the culprit, but it's not.我制作了一个简单的测试应用程序,它只创建一个 Win32 按钮来查看 Qt 是否是罪魁祸首,但事实并非如此。

This is the HwndHost implementation I have:这是我拥有的HwndHost实现:

using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace WpfHwndHostLayeredWindow
{
    class NativeButton : HwndHost
    {
        protected override HandleRef BuildWindowCore(HandleRef hwndParent)
        {
            var hwnd = PInvoke.User32.CreateWindowEx(
                0, // EX_STYLE
                "BUTTON",
                "Win32 Button",
                PInvoke.User32.WindowStyles.WS_CHILD,
                0, 0, 100, 100,
                hwndParent.Handle,
                IntPtr.Zero, // hMenu
                new IntPtr(PInvoke.User32.GetWindowLong(hwndParent.Handle, PInvoke.User32.WindowLongIndexFlags.GWLP_HINSTANCE)),
                IntPtr.Zero // lParam
            );

            return new HandleRef(this, hwnd);
        }

        protected override void DestroyWindowCore(HandleRef hwnd)
        {
            PInvoke.User32.DestroyWindow(hwnd.Handle);
        }
    }
}

And the XAML for the WPF window:以及 WPF 窗口的 XAML:

<Window x:Class="WpfHwndHostLayeredWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfHwndHostLayeredWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        WindowStyle="None" ResizeMode="CanResizeWithGrip" AllowsTransparency="True">
    <Grid>
        <TextBlock Text="If you see this, the native control doesn't render properly"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"/>

        <local:NativeButton Margin="16"/>
    </Grid>
</Window>

I've tried setting the WS_EX_LAYERED flag but it didn't do anything.我试过设置WS_EX_LAYERED标志,但它没有做任何事情。

Is there any way to make it work or is it a (known) limitation of WPF/HwndHost?有什么方法可以使它工作还是WPF/HwndHost的(已知)限制?

The layered window is a window that draws its content off-screen.分层窗口是在屏幕外绘制其内容的窗口。 That means the system automatically composes and repaints layered windows and the windows of underlying applications.这意味着系统会自动组合和重新绘制分层窗口和底层应用程序的窗口。

If we embed a native window into a layered window, the child window content may not be painted due to the window types conflict in some environments.如果我们将原生窗口嵌入到分层窗口中,在某些环境下,由于窗口类型冲突,子窗口内容可能无法绘制。

See https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#layered-windows请参阅https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#layered-windows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM