天天看點

[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);

}

繼續閱讀