日前,有人问道:如何把一个子窗口设置为主窗口的“控制台”,也就是说,要在它上面进行一些系统性的操作。比如:功能的划分,子功能的调用。如果这样做出来的话,那么,它就具有操作直观性了。
好了,废话不说了,进入正题吧:)
我们用一种方法:加一个子窗口,并设置该子窗口为最底层,在该子窗口上加一个可拉伸的图片框。当该子窗口被激活,就把它设置为最底层。并且,不允许用户关闭它,就可以了。
具体方法:
加载一个子窗口(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
{
///
1<summary>
2/// Form1 的摘要说明。
3/// </summary>
public class Form1 : System.Windows.Forms.Form
{
///
1<summary>
2/// 必需的设计器变量。
3/// </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 调用后添加任何构造函数代码
//
} ** _
_**///
1<summary>
2/// 清理所有正在使用的资源。
3/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} ** _
_**#region Windows 窗体设计器生成的代码
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </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 ** _
_**///
1<summary>
2/// 应用程序的主入口点。
3/// </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
{
///
1<summary>
2/// Form2 的摘要说明。
3/// </summary>
public class Form2 : System.Windows.Forms.Form
{
///
1<summary>
2/// 必需的设计器变量。
3/// </summary>
private System.ComponentModel.Container components = null; _
_public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); _
_//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} _
_///
1<summary>
2/// 清理所有正在使用的资源。
3/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} _
_#region Windows 窗体设计器生成的代码
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </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
{
///
1<summary>
2/// Form3 的摘要说明。
3/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
///
1<summary>
2/// 必需的设计器变量。
3/// </summary>
private System.ComponentModel.Container components = null; **
**public Form3()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); **
**//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} **
**///
1<summary>
2/// 清理所有正在使用的资源。
3/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} **
**#region Windows 窗体设计器生成的代码
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </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("单击测试二!");
}
}
} **
好了。大体就是这些了。:)