WPF Win32 API 嵌入Form 窗体

 

WIn32 API:

 

public class Win32Native
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]
        public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);


        public const UInt32 WS_POPUP = 0x80000000;

        //assorted constants needed

        public static int GWL_STYLE = -16;

        public static int WS_CHILD = 0x40000000; //child window

        public static int WS_BORDER = 0x00800000; //window with border

        public static int WS_DLGFRAME = 0x00400000; //window with double border but no title

        public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar



        public const UInt32 WS_THICKFRAME = 0x40000;





        public const UInt32 WS_SIZEBOX = WS_THICKFRAME;
    }
View Code

 

public class EmbeddedApp
    {
        [DllImport("user32.dll")]
        public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);

        [DllImport("user32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
                    int X, int Y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool ShowWindow(IntPtr hWnd, short State);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool MoveWindow(IntPtr hWnd, int x,int y,int cx,int cy,bool repaint);


        public const int HWND_TOP = 0x0;
        public const int WM_COMMAND = 0x0112;
        public const int WM_QT_PAINT = 0xC2DC;
        public const int WM_PAINT = 0x000F;
        public const int WM_SIZE = 0x0005;
        public const int SWP_FRAMECHANGED = 0x0020;
    }
View Code

 

 

UserControl:

 

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public Panel PanlParent
        {
            get { return this.panel1; }
        }
    }
View Code

 

 

WPF 代码:

前台:

<Window x:Class="WpfApplicationWin32.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:WpfApplicationWin32"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Name="btnOPen" Content="打开" Height="23" Width="100" Click="btnOPen_Click"></Button>
        <Grid Grid.Row="1">
            <WindowsFormsHost  Name="windowsFormsHost1" SizeChanged="windowsFormsHost1_SizeChanged">
                <wf:UserControl1 x:Name="myUCParent"></wf:UserControl1>
            </WindowsFormsHost>
        </Grid>
    </Grid>
</Window>
View Code

后台:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
           
        }
        Form1 f1;
        IntPtr intptrChild = IntPtr.Zero;
        private void btnOPen_Click(object sender, RoutedEventArgs e)
        {
            IntPtr intptrParent = myUCParent.PanlParent.Handle;
            //
            f1 = new Form1();
            f1.Show();
            intptrChild = f1.Handle;
            // 
            Thread tt = new Thread(() =>
            {
                while (true)
                {
                    if (intptrChild != IntPtr.Zero)
                    {
                        this.Dispatcher.Invoke(new Action(() =>
                        { 
                            EmbeddedApp.SetParent(intptrChild, intptrParent);
                            EmbeddedApp.MoveWindow(intptrChild, 0, 0, myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
                            EmbeddedApp.ShowWindow(intptrChild, 5); 
                        }));

                        break;
                    }

                }
            });

            tt.IsBackground = true;
            tt.Start();
        }

        private void windowsFormsHost1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (f1 == null) return;
            EmbeddedApp.MoveWindow(f1.Handle, 0, 0, myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
        }
    }
View Code

 

转载请注明出处,谢谢!

 

posted @ 2015-07-02 11:18  天王星天  阅读(743)  评论(0编辑  收藏  举报