WPF實現MDI窗體的方法(父窗體中開啟嵌入的子窗體)
阿新 • • 發佈:2022-03-09
第一:新建一個類
類檔名稱為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); } }
第二:新建兩個窗體
窗體1:Window1.xaml
窗體2: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); w2.WindowState = WindowState.Maximized; //視窗最大化 }
第五:執行效果
父窗體:點選按鈕
實現的效果:父窗體中開啟子窗體
本文引自:https://blog.csdn.net/lassewang/article/details/7041855