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

 

第二:新建兩個窗體

        窗體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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM