L
lurker
Unregistered / Unconfirmed
GUEST, unregistred user!
网上找到一个例子,但是很难用Delphi实现,所以请高手帮帮忙啊。
http://www.agilelabs.cn/blogs/woody/archive/2005/12/29/399.aspx
内容摘抄如下:
一个多文档界面与TabControl的妙用,可以用做图形设计器的界面,有较好的用户体验.最终效果如下:
当点击TabControl相应页的时候,相应的子窗体呈现出来(一个子窗体对应一个Tab页).关闭子窗体,增加子窗体的时候,TabControl的Tab页相应减少,增加.实现过程如下:
将MainForm的IsMidContainer设为True,添加相应菜单,一个TabControl控件,删除其所有默认页.添加子窗体:ChildForm,ChildForm中的代码如下:
public partial class ChildForm : Form
{
/// <summary>
/// 该子窗体属于的Tab控件
/// </summary>
private TabControl tabControl;
/// <summary>
/// 该子窗体所属Tab控件的Tab页
/// </summary>
private TabPage tabPage;
public TabControl BelongsToTabCtrl
{
get { return this.tabControl; }
set { this.tabControl = value; }
}
public TabPage BelongsToTabPage
{
get { return this.tabPage; }
set { this.tabPage = value; }
}
public ChildForm()
{
InitializeComponent();
}
/// <summary>
/// 当子窗体激活的时候,选中相应的Tab页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChildForm_Activated(object sender, EventArgs e)
{
this.tabControl.SelectedTab = this.tabPage;
}
}
MainForm中的控制代码如下:
public partial class MainForm : Form
{
/// <summary>
/// 子窗体的个数
/// </summary>
int childCount = 0;
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 新建一个子窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
//新建一个子窗体
ChildForm childForm = new ChildForm();
childCount++;
childForm.Text = "ChildForm" + childCount;
childForm.MdiParent = this;
//将该子窗体所属于的TabControl设为该窗体中的TabControl
childForm.BelongsToTabCtrl = this.tabControl1;
TabPage tp = new TabPage();//新建一个Tab页
tp.Parent = this.tabControl1;//设置子窗体所属的TabControl
tp.Text = childForm.Text;
childForm.BelongsToTabPage = tp; //设置子窗体所属的Tab页
tp.Show();
childForm.Show();
this.tabControl1.SelectedTab = tp;
}
/// <summary>
/// 选择不同的Tab页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ChildForm childForm in this.MdiChildren)
{
if(childForm.BelongsToTabPage.Equals(tabControl1.SelectedTab))
{
//选中相应的子窗体
childForm.Select();
}
}
}
}
另外,可以在MainForm中调用:
this.LayoutMdi(MdiLayout.Cascade);
this.LayoutMdi(MdiLayout.TileHorizontal);
this.LayoutMdi(MdiLayout.TileVertical);
来改变子窗体在父窗体的布局.
http://www.agilelabs.cn/blogs/woody/archive/2005/12/29/399.aspx
内容摘抄如下:
一个多文档界面与TabControl的妙用,可以用做图形设计器的界面,有较好的用户体验.最终效果如下:
当点击TabControl相应页的时候,相应的子窗体呈现出来(一个子窗体对应一个Tab页).关闭子窗体,增加子窗体的时候,TabControl的Tab页相应减少,增加.实现过程如下:
将MainForm的IsMidContainer设为True,添加相应菜单,一个TabControl控件,删除其所有默认页.添加子窗体:ChildForm,ChildForm中的代码如下:
public partial class ChildForm : Form
{
/// <summary>
/// 该子窗体属于的Tab控件
/// </summary>
private TabControl tabControl;
/// <summary>
/// 该子窗体所属Tab控件的Tab页
/// </summary>
private TabPage tabPage;
public TabControl BelongsToTabCtrl
{
get { return this.tabControl; }
set { this.tabControl = value; }
}
public TabPage BelongsToTabPage
{
get { return this.tabPage; }
set { this.tabPage = value; }
}
public ChildForm()
{
InitializeComponent();
}
/// <summary>
/// 当子窗体激活的时候,选中相应的Tab页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChildForm_Activated(object sender, EventArgs e)
{
this.tabControl.SelectedTab = this.tabPage;
}
}
MainForm中的控制代码如下:
public partial class MainForm : Form
{
/// <summary>
/// 子窗体的个数
/// </summary>
int childCount = 0;
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 新建一个子窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
//新建一个子窗体
ChildForm childForm = new ChildForm();
childCount++;
childForm.Text = "ChildForm" + childCount;
childForm.MdiParent = this;
//将该子窗体所属于的TabControl设为该窗体中的TabControl
childForm.BelongsToTabCtrl = this.tabControl1;
TabPage tp = new TabPage();//新建一个Tab页
tp.Parent = this.tabControl1;//设置子窗体所属的TabControl
tp.Text = childForm.Text;
childForm.BelongsToTabPage = tp; //设置子窗体所属的Tab页
tp.Show();
childForm.Show();
this.tabControl1.SelectedTab = tp;
}
/// <summary>
/// 选择不同的Tab页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ChildForm childForm in this.MdiChildren)
{
if(childForm.BelongsToTabPage.Equals(tabControl1.SelectedTab))
{
//选中相应的子窗体
childForm.Select();
}
}
}
}
另外,可以在MainForm中调用:
this.LayoutMdi(MdiLayout.Cascade);
this.LayoutMdi(MdiLayout.TileHorizontal);
this.LayoutMdi(MdiLayout.TileVertical);
来改变子窗体在父窗体的布局.