WPF實現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); 

    testMdiWindow.WindowState = WindowState.Maximized;//加上這句可實現窗口加載時最大化,注意語句位置
}

 WinForms實現方法較簡單一些,

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


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/dotkit/archive/2009/11/11/4799055.aspx

 


免責聲明!

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



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