C#的WinForms,WPF互操作 [英] C# Winforms-WPF interop

查看:219
本文介绍了C#的WinForms,WPF互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个WinForms应用程序,它依赖于透光性好效果。然而,这被证明是背后的preverial一个绝对的痛苦!作为的WinForms林学习不处理的透明度特别好。

Currently I have a Winforms app which relies on transpareny effects. However this is proving to be an absolute pain in the preverial behind! Winforms as Im learning doesn't deal with transparency particularly well.

我不知道这是否将是透明位为任何容易使用WPF组件和WinForms休息(注althought Id喜欢在移动整个应用WPF这只是不feasable!)。我知道旁边没有关于WPF,因此,其原因我在这里!我是什么considereing是:

I was wondering whether or not this would be any easier using WPF components for the transparency bit and winforms for the rest (note althought Id like to move the whole app over to WPF this just isn't feasable!). I know next to nothing about WPF, hence the reason Im here! What I was considereing was :

一个WinForms用户控制在1个)举办一个WPF组件例如WPF控件的例子:

1) Host a WPF component within a Winforms User Control e.g. Example of WPF Control:

<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的控件的父代码。

2) Host a Winforms user control (content) within the white rectangle (dialog) of the WPF control. 3) allow the content (Winforms user control) to call code on the parent of the WPF-Control.

首要的事情...


  • 这是做还是一件合理的事我会叫错了树?

  • 才能实现这一目标在一个更简单的方式?

  • 谁能帮助我在这里? (示例代码将受到欢迎!)

  • 最后...是否有任何网上资源,可以帮助我)学习WPF和b)变得更加自给自足?

  • Is this a reasonable thing to do or am I barking up the wrong tree?
  • Can this be achieved in an easier fashion?
  • Can anyone help me here? (Sample code would be gratefully received!)
  • Finally ... are there any online resources that can help me a) learn WPF and b) become more self-sufficient?

推荐答案

下面是我曾经在手解决问题的解决方案。该解决方案依赖于覆盖控制渲染其父母作为位图图像。然后,这被描绘成是叠加控制的背景。

Here is the solution I used to solve the problem at Hand. This solution relies on the overlaying Control to render its Parent as a bitmap image. This then gets painted as the background of the overlaying control.

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控件根据需要渲染。目前还没有提供更新当父的变化,但是,它应该是微不足道的创建清除位图,并重新绘制Overlayng控制的自定义方法的位图。不是一个完美的解决方案我知道...但它似乎工作不够好。

This is performed purely within the WinForms domain, however I believe it could be possible to pass this Bitmap image to a WPF control to render as required. Currently there is no provision for updating the Bitmap when the parent changes, However, it should be trivial to create a custom method that clears the bitmap and re-draws the Overlayng control. Not an elegant solution I realise... but it appears to work well enough.

这篇关于C#的WinForms,WPF互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆