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