WPF实现MDI窗体的方法

本文介绍了如何在WPF应用中实现MDI(Multiple Document Interface)窗体。通过创建一个名为Win32Native的类,利用DllImport导入user32.dll库的SetParent方法,将Window2设置为Window1的子窗口。详细步骤包括创建Win32Native类,新建Window1和Window2两个窗体,在Window1中放置按钮并绑定事件,实现子窗口的定位。此外,对比了WinForms中实现MDI窗体的简单方法。

 

(效果不是很好,仅供参考)

第一:新建一个类(Class)

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

 

 WinForms实现方法较简单一些, 

private void button1_Click(object sender, RoutedEventArgs e)
{
    Window2 w2 = new Window2();
    w2.MdiParent = this;
    w2.Show();

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值