一.运行时效果
二.引子
我们通常要么在MDI窗体中处理类似的问题,要么采用设置toplevel = false的方式,将FORM做为control来add到容器中。不过,如果我们在add 到控件容器中的时候,form的标题栏是不能够激活的(也就是FORM被选中时,并没有高亮显示标题栏),那么如何可以达到这种效果呢,我想到了使用API函数SetParent,本文就是创建一个这样的控件容器。由于昨天写的匆忙,竟然没有粘贴到源代码,现在补上.特此道歉!呵呵.
三.源代码
1)控件代码
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace JcsExpLibary.JcsFormContainer
... {
public partial class JcsFormContainer :Panel
...{
private List<Form> FormList;
public JcsFormContainer()
...{
InitializeComponent();
FormList = new List<Form>();
this.AutoScroll = true;
this.HScroll = true;
this.VScroll = true ;
}
public void SetParentContainer(Control c)
...{
if (c!=null && c is Form)
...{
Form frm = c as Form;
frm.WindowState = FormWindowState.Normal;
frm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
WIN32.SetParent(frm.Handle, this.Handle);
this.FormList.Add(frm);
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
//c.MaximumSize = new Size(this.ClientRectangle.Width ,this.ClientRectangle.Height );
c.Show();
}
}
void frm_FormClosed(object sender, FormClosedEventArgs e)
...{
this.FormList.Remove(sender as Form);
}
//protected override void OnResize(EventArgs e)
//{
// base.OnResize(e);
// //foreach (Form frm in this.FormList)
// //{
// // //frm.MaximumSize = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
// //}
//}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace JcsExpLibary.JcsFormContainer
... {
public partial class JcsFormContainer :Panel
...{
private List<Form> FormList;
public JcsFormContainer()
...{
InitializeComponent();
FormList = new List<Form>();
this.AutoScroll = true;
this.HScroll = true;
this.VScroll = true ;
}
public void SetParentContainer(Control c)
...{
if (c!=null && c is Form)
...{
Form frm = c as Form;
frm.WindowState = FormWindowState.Normal;
frm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
WIN32.SetParent(frm.Handle, this.Handle);
this.FormList.Add(frm);
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
//c.MaximumSize = new Size(this.ClientRectangle.Width ,this.ClientRectangle.Height );
c.Show();
}
}
void frm_FormClosed(object sender, FormClosedEventArgs e)
...{
this.FormList.Remove(sender as Form);
}
//protected override void OnResize(EventArgs e)
//{
// base.OnResize(e);
// //foreach (Form frm in this.FormList)
// //{
// // //frm.MaximumSize = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
// //}
//}
}
}
2)类代码
using
System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace JcsExpLibary.JcsFormContainer
... {
public class WIN32
...{
/**//// <summary>
/// 设置控件/窗体的父级容器
/// </summary>
/// <param name="childWindow"></param>
/// <param name="newParent"></param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr childWindow, IntPtr newParent);
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace JcsExpLibary.JcsFormContainer
... {
public class WIN32
...{
/**//// <summary>
/// 设置控件/窗体的父级容器
/// </summary>
/// <param name="childWindow"></param>
/// <param name="newParent"></param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr childWindow, IntPtr newParent);
}
}
3)调用测试代码
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
... {
public partial class Form11 : Form
...{
public Form11()
...{
InitializeComponent();
//Form9 frm = new Form9();
//frm.MdiParent = this;
//frm.Show();
Form10 frm = new Form10();
Form10 frm1 = new Form10();
this.jcsFormContainer1.SetParentContainer(frm);
this.jcsFormContainer1.SetParentContainer(frm1);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
... {
public partial class Form11 : Form
...{
public Form11()
...{
InitializeComponent();
//Form9 frm = new Form9();
//frm.MdiParent = this;
//frm.Show();
Form10 frm = new Form10();
Form10 frm1 = new Form10();
this.jcsFormContainer1.SetParentContainer(frm);
this.jcsFormContainer1.SetParentContainer(frm1);
}
}
}