日前,有人問道:如何把一個子窗口設置為主窗口的“控制臺”,也就是說,要在它上面進行一些系統性的操作。比如:功能的劃分,子功能的調用。如果這樣做出來的話,那么,它就具有操作直觀性了。 好了,廢話不說了,進入正題吧:)
我們用一種方法:加一個子窗口,并設置該子窗口為最底層,在該子窗口上加一個可拉伸的圖片框。當該子窗口被激活,就把它設置為最底層。并且,不允許用戶關閉它,就可以了。 具體方法: 加載一個子窗口(form1),并重寫該子窗口的OnActivated事件: protected override void OnActivated(EventArgs e) { SendToBack(); } =================== 在該子窗口上加上你要的PictureBox 在主窗口中加載一個空窗口:Form mdichile = null; 寫主窗口的MdiChildActiveate事件: private void MainForm_MdiChildActivate(object sender, System.EventArgs e) { Form form = this.ActiveMdiChild; if(form != null) { if(form is form1) { if(mdichile != null) { mdichile.Activate(); } } else { mdichile = form; } } else { form1.Activate(); } } ================== 這就可以了。:-)
下面是它的源代碼: 源代碼Form1(主窗口): using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace Demo { /// <summary> /// Form1 的摘要說明。 /// </summary> public class Form1 : System.Windows.Forms.Form { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private Form form = null; private Form3 form3 = new Form3();
public Form1() { // // Windows 窗體設計器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // }
/// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2}); this.menuItem1.Text = "123"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "123"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(508, 302); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "Form1"; this.MdiChildActivate += new System.EventHandler(this.Form1_MdiChildActivate); this.Load += new System.EventHandler(this.Form1_Load);
} #endregion
/// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void menuItem2_Click(object sender, System.EventArgs e) { Form2 form = new Form2(); form.MdiParent = this; form.Show(); }
private void Form1_MdiChildActivate(object sender, System.EventArgs e) { Form theform = this.ActiveMdiChild; if(theform != null) { if(theform is Form3) { if(form != null) { this.form.Activate(); } } else { form = theform; } } else { form3.Activate(); } }
private void Form1_Load(object sender, System.EventArgs e) { form3.MdiParent = this; form3.Show(); } } } ================== Form2: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;
namespace Demo { /// <summary> /// Form2 的摘要說明。 /// </summary> public class Form2 : System.Windows.Forms.Form { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null;
public Form2() { // // Windows 窗體設計器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // }
/// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form2"; } #endregion } } ============================= Form3(底層窗口): using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;
namespace Demo { /// <summary> /// Form3 的摘要說明。 /// </summary> public class Form3 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null;
public Form3() { // // Windows 窗體設計器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // }
/// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 32); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(16, 72); this.button2.Name = "button2"; this.button2.TabIndex = 2; this.button2.Text = "button2"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form3 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form3"; this.Text = "Form3"; this.ResumeLayout(false);
} #endregion protected override void OnActivated(EventArgs e) { SendToBack(); }
private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show("單擊測試一!"); }
private void button2_Click(object sender, System.EventArgs e) { MessageBox.Show("單擊測試二!"); } } } 好了。大體就是這些了。:)
|