Csharp/CSharp Tutorial/GUI Windows Forms/MDI
Содержание
Arrange Child form
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class MDIFormArrange : System.Windows.Forms.Form {
private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileExit; private System.Windows.Forms.MenuItem mnuWindow; private System.Windows.Forms.MenuItem mnuArrange; private System.Windows.Forms.MenuItem mnuArrangeCascade; private System.Windows.Forms.MenuItem mnuArrangeVert; private System.Windows.Forms.MenuItem mnuArrangeHorizontal; private System.Windows.Forms.MenuItem mnuFileNew; private System.ruponentModel.Container components = null; public MDIFormArrange() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuFileNew = new System.Windows.Forms.MenuItem(); this.mnuFileExit = new System.Windows.Forms.MenuItem(); this.mnuWindow = new System.Windows.Forms.MenuItem(); this.mnuArrange = new System.Windows.Forms.MenuItem(); this.mnuArrangeCascade = new System.Windows.Forms.MenuItem(); this.mnuArrangeVert = new System.Windows.Forms.MenuItem(); this.mnuArrangeHorizontal = new System.Windows.Forms.MenuItem(); // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuWindow, this.mnuArrange}); // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFileNew, this.mnuFileExit}); this.mnuFile.Text = "&File"; // this.mnuFileNew.Index = 0; this.mnuFileNew.Text = "&New"; this.mnuFileNew.Click += new System.EventHandler(this.mnuFileNew_Click); // this.mnuFileExit.Index = 1; this.mnuFileExit.Text = "E&xit"; this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click); // this.mnuWindow.Index = 1; this.mnuWindow.MdiList = true; this.mnuWindow.Text = "&Window"; // this.mnuArrange.Index = 2; this.mnuArrange.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuArrangeCascade, this.mnuArrangeVert, this.mnuArrangeHorizontal}); this.mnuArrange.Text = "&Arrange Window"; // this.mnuArrangeCascade.Index = 0; this.mnuArrangeCascade.Text = "&Cascade"; this.mnuArrangeCascade.Click += new System.EventHandler(this.mnuArrangeCascade_Click); // this.mnuArrangeVert.Index = 1; this.mnuArrangeVert.Text = "&Vertical"; this.mnuArrangeVert.Click += new System.EventHandler(this.mnuArrangeVert_Click); // this.mnuArrangeHorizontal.Index = 2; this.mnuArrangeHorizontal.Text = "&Horizontal"; this.mnuArrangeHorizontal.Click += new System.EventHandler(this.mnuArrangeHorizontal_Click); // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 269); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "MDIFormArrange"; this.Text = "The MDI Application"; } [STAThread] static void Main() { Application.Run(new MDIFormArrange()); } private void mnuFileExit_Click(object sender, System.EventArgs e) { this.Close(); } private void mnuArrangeCascade_Click(object sender, System.EventArgs e) { LayoutMdi(MdiLayout.Cascade); } private void mnuArrangeVert_Click(object sender, System.EventArgs e) { LayoutMdi(MdiLayout.TileVertical); } private void mnuArrangeHorizontal_Click(object sender, System.EventArgs e) { LayoutMdi(MdiLayout.TileHorizontal); } private void mnuFileNew_Click(object sender, System.EventArgs e) { MDIChildForm newChild = new MDIChildForm(); newChild.MdiParent = this; newChild.Show(); }
} public class MDIChildForm : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public MDIChildForm() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 269); }
}</source>
Is a form Mdi Container
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class ChildPrototypeForm : Form {
public ChildPrototypeForm() { InitializeComponent(); } private void ChildPrototypeForm_Click(object sender, EventArgs e) { int r, g, b; Random ran = new Random(); r = ran.Next(0, 255); g = ran.Next(0, 255); b = ran.Next(0, 255); Color currColor = Color.FromArgb(r, g, b); this.BackColor = currColor; this.Text = currColor.ToString(); } private void InitializeComponent() { this.Click += new System.EventHandler(this.ChildPrototypeForm_Click); }
} public class MainWindow : Form {
public MainWindow() { InitializeComponent(); } private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.Cascade); } private void verticalToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.TileVertical); } private void horizontalToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.TileHorizontal); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { ChildPrototypeForm newChild = new ChildPrototypeForm(); newChild.MdiParent = this; newChild.Show(); } private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.windowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.arrangeWindowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.cascadeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.verticalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.horizontalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem1, this.windowToolStripMenuItem, this.arrangeWindowsToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.MdiWindowListItem = this.windowToolStripMenuItem; this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(440, 24); this.menuStrip1.TabIndex = 2; this.menuStrip1.Text = "menuStrip1"; // // fileToolStripMenuItem1 // this.fileToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.newToolStripMenuItem, this.exitToolStripMenuItem}); this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1"; this.fileToolStripMenuItem1.Text = "&File"; // // newToolStripMenuItem // this.newToolStripMenuItem.Name = "newToolStripMenuItem"; this.newToolStripMenuItem.Text = "&New"; this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click); // // exitToolStripMenuItem // this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Text = "E&xit"; this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // windowToolStripMenuItem // this.windowToolStripMenuItem.Name = "windowToolStripMenuItem"; this.windowToolStripMenuItem.Text = "&Window"; // // arrangeWindowsToolStripMenuItem // this.arrangeWindowsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.cascadeToolStripMenuItem, this.verticalToolStripMenuItem, this.horizontalToolStripMenuItem}); this.arrangeWindowsToolStripMenuItem.Name = "arrangeWindowsToolStripMenuItem"; this.arrangeWindowsToolStripMenuItem.Text = "&Arrange Windows"; // // cascadeToolStripMenuItem // this.cascadeToolStripMenuItem.Name = "cascadeToolStripMenuItem"; this.cascadeToolStripMenuItem.Text = "&Cascade"; this.cascadeToolStripMenuItem.Click += new System.EventHandler(this.cascadeToolStripMenuItem_Click); // // verticalToolStripMenuItem // this.verticalToolStripMenuItem.Name = "verticalToolStripMenuItem"; this.verticalToolStripMenuItem.Text = "&Vertical"; this.verticalToolStripMenuItem.Click += new System.EventHandler(this.verticalToolStripMenuItem_Click); // // horizontalToolStripMenuItem // this.horizontalToolStripMenuItem.Name = "horizontalToolStripMenuItem"; this.horizontalToolStripMenuItem.Text = "&Horizontal"; this.horizontalToolStripMenuItem.Click += new System.EventHandler(this.horizontalToolStripMenuItem_Click); // // MainWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(440, 238); this.Controls.Add(this.menuStrip1); this.IsMdiContainer = true; this.Name = "MainWindow"; this.Text = "My Mdi Application"; this.menuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); }
private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem windowToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem arrangeWindowsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem cascadeToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem verticalToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem horizontalToolStripMenuItem; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainWindow()); }
}</source>
MDI children form background
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public partial class ChildForm : Form {
public ChildForm() { InitializeComponent(); } private void settoRedToolStripMenuItem_Click(object sender, EventArgs e) { this.BackColor = Color.Red; } private void settoBlueToolStripMenuItem_Click(object sender, EventArgs e) { this.BackColor = Color.Blue; } private void settoGreenToolStripMenuItem_Click(object sender, EventArgs e) { this.BackColor = Color.Green; } public void Save() { MessageBox.Show("I have saved my data!"); }
} partial class ChildForm {
private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.specialToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.settoRedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.settoBlueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.settoGreenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.specialToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(534, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // specialToolStripMenuItem // this.specialToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.settoRedToolStripMenuItem, this.settoBlueToolStripMenuItem, this.settoGreenToolStripMenuItem}); this.specialToolStripMenuItem.Name = "specialToolStripMenuItem"; this.specialToolStripMenuItem.Text = "&Special"; // // settoRedToolStripMenuItem // this.settoRedToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.settoRedToolStripMenuItem.Name = "settoRedToolStripMenuItem"; this.settoRedToolStripMenuItem.Text = "Set to Red"; this.settoRedToolStripMenuItem.Click += new System.EventHandler(this.settoRedToolStripMenuItem_Click); // // settoBlueToolStripMenuItem // this.settoBlueToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.settoBlueToolStripMenuItem.Name = "settoBlueToolStripMenuItem"; this.settoBlueToolStripMenuItem.Text = "Set to Blue"; this.settoBlueToolStripMenuItem.Click += new System.EventHandler(this.settoBlueToolStripMenuItem_Click); // // settoGreenToolStripMenuItem // this.settoGreenToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.settoGreenToolStripMenuItem.Name = "settoGreenToolStripMenuItem"; this.settoGreenToolStripMenuItem.Text = "Set to Green"; this.settoGreenToolStripMenuItem.Click += new System.EventHandler(this.settoGreenToolStripMenuItem_Click); // // ChildForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(534, 541); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "ChildForm"; this.Text = "ChildForm"; this.menuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem specialToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem settoRedToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem settoBlueToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem settoGreenToolStripMenuItem;
} public partial class Form1 : Form {
public Form1() { InitializeComponent(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { ChildForm child = new ChildForm(); child.MdiParent = this; child.Show(); }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { ChildForm formToSave = (ChildForm)this.ActiveMdiChild; formToSave.Save(); } private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e) { if (this.MdiChildren.Length == 0) saveToolStripMenuItem.Enabled = false; else saveToolStripMenuItem.Enabled = true; }
} partial class Form1 {
private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(576, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // fileToolStripMenuItem // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.newToolStripMenuItem, this.saveToolStripMenuItem, this.toolStripSeparator1, this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Text = "&File"; this.fileToolStripMenuItem.DropDownOpening += new System.EventHandler(this.fileToolStripMenuItem_DropDownOpening); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; // // newToolStripMenuItem // this.newToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.newToolStripMenuItem.Name = "newToolStripMenuItem"; this.newToolStripMenuItem.Text = "&New"; this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click); // // saveToolStripMenuItem // this.saveToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Text = "&Save"; this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); // // exitToolStripMenuItem // this.exitToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Text = "E&xit"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(576, 438); this.Controls.Add(this.menuStrip1); this.IsMdiContainer = true; this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "Form1"; this.menuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
} public class ChildFormBackGround {
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}</source>
Set MdiParent
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Windows.Forms;
class frmContainer : Form {
public frmContainer() { InitializeComponent(); frmChild child = new frmChild(this); child.Show(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.IsMdiContainer = true; this.Name = "frmContainer"; this.Text = "MDI Basic"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; }
} class frmChild : Form {
public frmChild(frmContainer parent) { InitializeComponent(); this.MdiParent = parent; } private void InitializeComponent() { this.Text = "frmChild"; } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new frmContainer()); }
}</source>