8

我有一个想要覆盖到 WinForms 应用程序的 WPF 控件。所以我尽职尽责地创建了一个可以显示以下 WPF 对象的元素主机:

<UserControl x:Class="LightBoxTest.LightBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="Transparent">
    <Grid Name="dialogHolder" Background="Transparent" Opacity="1">
        <Rectangle Name="rectangle1" Stroke="White" Fill="Black" RadiusX="10" RadiusY="10" Opacity="0.5" />
        <StackPanel Name="stackPanel1" Background="Transparent" Height="300" VerticalAlignment="Top">
            <Rectangle Name="spacer" Opacity="0" Stroke="Gray" Fill="White" RadiusX="10" RadiusY="10" Height="100" Width="300" />
            <Grid Height="100" Name="contentHolder" Width="250">
                <Rectangle Name="dialog" Stroke="Gray" Fill="White" RadiusX="10" RadiusY="10" Height="100" Width="250" />
            </Grid>
        </StackPanel>
    </Grid>
</UserControl>

问题是 WinForm 窗体上的控件不呈现,而 WPF 只是在屏幕上消除它们。

元素主机的创建方式如下:

dialogHost = new ElementHost(); 
dialogHost.Child = dialog;
dialogHost.BackColorTransparent = true;
dialogHost.BringToFront();  
dialogHost.Show();

有什么我应该做而我没有做的吗?

是否存在关于在 Winforms 上显示透明 WPF 控件的已知问题?

任何可能有帮助的文章?

注意:这个问题与这个问题有关

4

4 回答 4

7

我认为你遇到了空域问题。AFAIK,您不能混合 WPF 透明度和 ElementHost 透明度,因为 ElementHost 拥有空域。

链接中有一个关于创建非矩形 hwnd 来托管 WPF 内容的简短说明,这可能会让您走得更远。

也许您可以考虑将更多的 WinForms 应用程序迁移到 WPF?

于 2009-06-02T04:39:32.780 回答
2

您应该阅读以下内容:使用 ElementHost 时加载 wpf 控件之前的黑色背景 只需隐藏和显示它(不酷但有效)

于 2012-01-17T20:15:01.633 回答
0

这似乎是互操作空域问题。

于 2009-06-01T20:55:56.677 回答
-1

您可能已经尝试过了,但是如何在用户控件上设置不透明度呢?

于 2009-06-01T20:51:57.700 回答