簡體   English   中英

分層(透明)WPF 窗口中的 Win32 本機控件不可見

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

我想在 WPF 窗口中托管本機 Win32(Qt 小部件)控件。 我的問題是,如果我在普通 WPF 窗口中托管它,一切正常,但是當我將 WPF 窗口的AllowsTransparency設置為true ,將不再呈現本機內容。 我制作了一個簡單的測試應用程序,它只創建一個 Win32 按鈕來查看 Qt 是否是罪魁禍首,但事實並非如此。

這是我擁有的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);
        }
    }
}

以及 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>

我試過設置WS_EX_LAYERED標志,但它沒有做任何事情。

有什么方法可以使它工作還是WPF/HwndHost的(已知)限制?

分層窗口是在屏幕外繪制其內容的窗口。 這意味着系統會自動組合和重新繪制分層窗口和底層應用程序的窗口。

如果我們將原生窗口嵌入到分層窗口中,在某些環境下,由於窗口類型沖突,子窗口內容可能無法繪制。

請參閱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