WPF calls Win Form

Source: Internet
Author: User

WPF is the next-generation version of Win form, and now more and more companies are using WPF. How to be compatible with existing applications developed using win form? There are three ways to call win form in WPF.

Use the WindowsFormsHost control in WPF to host a control or control library for win form.

WindowsFormsHost is located in the Windowsformsintegration assembly, under the System.Windows.Forms.Integration namespace. This control can host either a single control of win form or a composite control, but cannot use the top-level form, or an error occurs.

<window x:class= "Wpftest.mainwindow"        xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:winform= "Clr-namespace: System.windows.forms;assembly=system.windows.forms "        title=" WPF Invoke WinForm ">    <Grid>        < grid.rowdefinitions>            <rowdefinition height= "Auto"/>            <rowdefinition height= "*"/>        </ grid.rowdefinitions>        <textblock text= "This was only a test of WPF invoking win form"/>        < WindowsFormsHost grid.row= "1" x:name= "WFH" >            <winform:button text= "I am from Win form" click= "Button_click_1 "/>        </WindowsFormsHost>    </Grid></Window>

  

Use Windowsinterophelper in WPF to locate and invoke the Win form form.

Windowsinterophelper is located in the PresentationFramework assembly, under the System.Windows.Interop namespace. It can get the form of WPF and use IWin32Window as the carrier of win form. The main code is as follows:

public class WindowWapper:System.Windows.Forms.IWin32Window    {        private IntPtr _handle;        Public IntPtr Handle        {            get            {                return this._handle;            }        }        Public Windowwapper (INTPTR handle)        {            this._handle = handle;        }    }
private void Firstwaytoinvoke ()        {            var form = WinformExample.Program.CreatInstance ();            Windowinterophelper helper = new Windowinterophelper (this);            Form. StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;            Form. Show (New Windowwapper (helper). Handle));        }

use process to directly start an existing win form application sequence.

The main use of the Process.Start () method is to start a process, the win form application.

private static void Secondwaytoinvoke (string winformexepath)        {            process.start (winformexepath);        }

Specific example: Http://files.cnblogs.com/files/allanli/WpfTest.zip

WPF calls Win Form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.