天天看点

[WPF]WPF实现MDI窗体的方法

说是WPF实现MDI窗体,其实只是一个只是封装了一下而于,目前还没有找到更好的方法,如果谁有的话还请多多指教:)

现实如此

第一:新建一个类

Win32Native.cs

代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace WpfApplication1

{

public class Win32Native

{

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]

public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr);

}

}

第二:新建二个窗体:

Window1.xaml

Window2.xaml

第三:Window1.xaml.cs中添加引用

using System.Windows.Interop;

第四:在Window1窗体中放上一个Button1

其事件如下:

private void button1_Click(object sender, RoutedEventArgs e)

{

Window2 w2 = new Window2();

w2.Show();

WindowInteropHelper parentHelper = new WindowInteropHelper(this);

WindowInteropHelper childHelper = new WindowInteropHelper(w2);

Win32Native.SetParent(childHelper.Handle, parentHelper.Handle);

}

继续阅读