2

目前我有一个依赖透明效果的 Winforms 应用程序。然而事实证明,这在过去的背后绝对是一种痛苦!我正在学习的 Winforms 并不能很好地处理透明度问题。

我想知道将WPF组件用于透明度位和winforms用于其余部分是否会更容易(请注意,尽管我想将整个应用程序移至WPF,但这是不可行的!)。我对 WPF 几乎一无所知,因此我在这里!我正在考虑的是:

1) 在 Winforms 用户控件中托管 WPF 组件,例如 WPF 控件示例:

<UserControl x:Class="WindowsFormsApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
         <Rectangle Name="rectangle1" Stroke="White" Fill="Black" RadiusX="10" RadiusY="10" Opacity="0.7" />
        <Rectangle Margin="57,101,43,99" Name="dialog" Stroke="Gray" Fill="White" RadiusX="10" RadiusY="10" />
    </Grid>
</UserControl>

2) 在 WPF 控件的白色矩形(对话框)内承载一个 Winforms 用户控件(内容)。3) 允许内容(Winforms 用户控件)调用 WPF-Control 父级上的代码。

第一件事首先...

  • 这是一个合理的做法还是我吠错了树?
  • 这可以以更简单的方式实现吗?
  • 有人能帮我一下吗?(示例代码将不胜感激!)
  • 最后......有没有任何在线资源可以帮助我a)学习WPF和b)变得更加自给自足?
4

2 回答 2

4

这当然是可能的,我认为你是对的,这将是实现透明度的最简单方法。

我自己没有尝试过,但是根据 CodeProject 上的这篇文章,它应该很简单。您应该使用ElementHost 控件来托管您的 WPF 内容。

在 WinForms 控件中托管 WPF 是一种受支持的方案,是框架中内置的一项功能。所以这样做应该没有问题。还有一个 WPF 组件用于另一种方式,将 WinForms 托管在 WPF 应用程序中。

于 2009-06-01T18:44:46.490 回答
1

这是我用来解决手头问题的解决方案。此解决方案依赖于覆盖控件将其父级呈现为位图图像。然后将其绘制为覆盖控件的背景。

public class OverlayingControl : UserControl
{
    /// <summary>
    /// Overrides the c# standard Paint Background to allow the custom background to be drawn 
    /// within the OnPaint function
    /// </summary>
    /// 
    /// <param name="e">Arguements used within this function</param>
    protected override void OnPaintBackground( PaintEventArgs e )
    {
        //Do Nothing 
    }

    protected override void OnPaint( PaintEventArgs e )
    {
        // Render the Parents image to a Bitmap. NB: bitmap dimensions and Parent Bounds can be changed to achieve the desitred effect
        Bitmap background = new Bitmap( Width, Height, PixelFormat.Format64bppArgb );
        Parent.DrawToBitmap( background, Parent.Bounds );

        // Paint background image             
        g.DrawImage( background, 0, 0, new RectangleF( Location, Size ), GraphicsUnit.Pixel );

        // Perform any alpha-blending here by drawing any desired overlay e.g.
        // g.FillRectangle( new SolidBrush( semiTransparentColor ), Bounds);
    }

}

这纯粹在 WinForms 域内执行,但是我相信可以将此位图图像传递给 WPF 控件以根据需要进行渲染。目前没有规定在父项更改时更新 Bitmap,但是,创建一个清除位图并重新绘制 Overlayng 控件的自定义方法应该是微不足道的。我意识到这不是一个优雅的解决方案......但它似乎运作良好。

于 2009-08-19T07:13:46.040 回答