public Form1() { InitializeComponent(); this.IsMdiContainer = true; }
我是直接把this.IsMdiContainer = true ;寫在窗體的構造函數中,你也能夠寫的form_load函數中。html
private void New_Click ( object sender , EventArgs e )//這是一個菜單的事件 { Form frmTemp = new Form ( ) ; //新建一個窗體 frmTemp.MdiParent = this ; //定義此窗體的父窗體,從而此窗體成爲一個MDI窗體 frmTemp.Text = "窗體0" + FormCount.ToString ( ) ; //設定MDI窗體的標題 FormCount++ ; frmTemp.Show ( ) ; //把此MDI窗體顯示出來 }
(3)實現對MDI窗體的層疊:
對於在主窗體中實現對MDI窗體的層疊操做,在主程序中,是經過一個方法來實現的,這個方法就是LayoutMdi,他所帶的參數是MdiLayout.Cascade,具體實現語句以下:
編程
private void Cascade_Click ( object sender , EventArgs e ) //實現對主窗體中的MDI窗體的層疊操做 { this.LayoutMdi ( MdiLayout.Cascade ) ; }
實現操做後具體以下圖:windows
this.LayoutMdi ( MdiLayout.TileHorizontal ) ;
this.LayoutMdi ( MdiLayout.TileVertical ) ;//實現對主窗體中的MDI窗體的垂直平鋪操做
using System ; using System.Windows.Forms ; using System.ComponentModel ; using System.Drawing ; //導入在程序中用到的名稱空間 class MDIDemo : Form { private static int FormCount = 1 ; //定義此常量是爲了統計MDI窗體數目, MainMenu mnuMain = new MainMenu ( ) ; MenuItem FileMenu ; MenuItem NewMenu ; MenuItem ExitMenu ; MenuItem WindowMenu ; public MDIDemo ( ) { this.IsMdiContainer = true ; this.Text = "MDI演示程序" ; FileMenu = new MenuItem ( ) ; FileMenu.Text = "文件" ; WindowMenu = new MenuItem ( ) ; WindowMenu.Text = "窗口(&W)" ; WindowMenu.MenuItems.Add ( "窗體層疊(&C)" , new EventHandler ( Cascade_Click ) ) ; WindowMenu.MenuItems.Add ( "水平平鋪(&H)" , new EventHandler ( TileH_Click ) ) ; WindowMenu.MenuItems.Add ( "垂直平鋪(&V)" , new EventHandler ( TileV_Click ) ) ; WindowMenu.MdiList = true ; //這一句比較重要,有了這一句就能夠實如今新建一個MDI窗體後會在此主菜單項下顯示存在的MDI窗體菜單項 NewMenu = new MenuItem ( ) ; NewMenu.Text = "新建窗體(&N)" ; NewMenu.Click += new EventHandler ( New_Click ) ; ExitMenu = new MenuItem ( ) ; ExitMenu.Text = "退出(&X)" ; ExitMenu.Click += new EventHandler ( Exit_Click ) ; FileMenu.MenuItems.Add ( NewMenu ) ; FileMenu.MenuItems.Add ( new MenuItem ( "-" ) ) ; FileMenu.MenuItems.Add ( ExitMenu ) ; mnuMain.MenuItems.Add ( FileMenu ) ; mnuMain.MenuItems.Add ( WindowMenu ) ; this.Menu = mnuMain ; } private void Cascade_Click ( object sender , EventArgs e ) //實現對主窗體中的MDI窗體的層疊操做 { this.LayoutMdi ( MdiLayout.Cascade ) ; } private void TileH_Click ( object sender , EventArgs e ) //實現對主窗體中的MDI窗體的水平平鋪操做 { this.LayoutMdi ( MdiLayout.TileHorizontal ) ; } private void TileV_Click ( object sender , EventArgs e ) //實現對主窗體中的MDI窗體的垂直平鋪操做 { this.LayoutMdi ( MdiLayout.TileVertical ) ; } private void New_Click ( object sender , EventArgs e ) { Form frmTemp = new Form ( ) ; //新建一個窗體 frmTemp.MdiParent = this ; //定義此窗體的父窗體,從而此窗體成爲一個MDI窗體 frmTemp.Text = "窗體0" + FormCount.ToString ( ) ; //設定MDI窗體的標題 FormCount++ ; frmTemp.Show ( ) ; //把此MDI窗體顯示出來 } private void Exit_Click ( object sender , EventArgs e ) { this.Dispose ( ); Application.Exit ( ) ; } public static void Main ( ) { Application.Run ( new MDIDemo ( ) ) ; } }
再通過如下編譯命令編譯過之後,就能夠獲得執行程序:
csc /t:winexe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll mdi.cs
瀏覽器
四.總結:
本文主要是介紹如何用Visual C#來實現對MDI窗體的編程。即:新建MDI窗體、MDI窗體層疊、MDI窗體水平平鋪、MDI窗體的垂直平鋪。經過上述介紹,可見,在Visual C#中處理MDI仍是至關比較簡單的。
不過說實如今我不建議你們這樣實現了,而是喜歡使用Tabcontrol的方法,這樣容易控件,並且能夠達到多任務,多操做的效果,也是目前幾乎全部瀏覽器都採用的方法 服務器