viual studio c#中如何建立mdi窗体 20
1个回答
展开全部
MDI父窗体属性MdiChildren是已显示MDI子窗体数组。遍历这个数组,可获取所有已显示的子窗体,进而获取各个子窗体的窗口句柄。
【例】Form1是MDI父窗体;Form2、Form3是MDI子窗体。Form1的窗体代码Form1.cs 如下
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 设置 Form1 为MDI父窗体
this.IsMdiContainer = true;
}
private void Form1_Load(object sender, EventArgs e)
{
// Form2是Form1的MDI子窗体
Form2 f2 = new Form2();
f2.MdiParent = this;
// 显示
f2.Show();
// Form3是Form1的MDI子窗体
Form3 f3 = new Form3();
f3.MdiParent = this;
// 显示
f3.Show();
// 遍历MdiChildren数组,获取所有子窗体的
// 窗体句柄
foreach(Form f in this.MdiChildren)
{
IntPtr h = f.Handle;
Console.WriteLine(h);
}
// 获取子窗体Form2的窗体句柄
foreach(Form f in this.MdiChildren)
{
if(f.Name == "Form2")
{
IntPtr h = f.Handle;
Console.WriteLine("子窗体Form2的句柄为 {0}", h);
break;
}
}
}
}
}
【例】Form1是MDI父窗体;Form2、Form3是MDI子窗体。Form1的窗体代码Form1.cs 如下
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 设置 Form1 为MDI父窗体
this.IsMdiContainer = true;
}
private void Form1_Load(object sender, EventArgs e)
{
// Form2是Form1的MDI子窗体
Form2 f2 = new Form2();
f2.MdiParent = this;
// 显示
f2.Show();
// Form3是Form1的MDI子窗体
Form3 f3 = new Form3();
f3.MdiParent = this;
// 显示
f3.Show();
// 遍历MdiChildren数组,获取所有子窗体的
// 窗体句柄
foreach(Form f in this.MdiChildren)
{
IntPtr h = f.Handle;
Console.WriteLine(h);
}
// 获取子窗体Form2的窗体句柄
foreach(Form f in this.MdiChildren)
{
if(f.Name == "Form2")
{
IntPtr h = f.Handle;
Console.WriteLine("子窗体Form2的句柄为 {0}", h);
break;
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询