DockPanel 右键增加关闭,除此之外全部关闭的功能

在项目中新建一个class文件,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WeifenLuo.WinFormsUI.Docking
{
public class DockContentEx : WeifenLuo.WinFormsUI.Docking.DockContent
{
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem 关闭ToolStripMenuItem;
private ToolStripMenuItem 全部关闭ToolStripMenuItem;
private ToolStripMenuItem 除此之外全部关闭ToolStripMenuItem;
private IContainer components;

public DockContentEx()
{
//ContextMenuStrip cms = new System.Windows.Forms.ContextMenuStrip();
//ToolStripMenuItem tsmiClose = new System.Windows.Forms.ToolStripMenuItem();
//ToolStripMenuItem tsmiCloseAll = new System.Windows.Forms.ToolStripMenuItem();

//tsmiClose.Name = "cms";
//tsmiClose.Size = new System.Drawing.Size(98, 22);
//tsmiClose.Text = "关闭";
//tsmiClose.Click += new System.EventHandler(this.tsmiClose_Click);

//cms.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {tsmiClose});
//cms.Name = "tsmiClose";
//cms.Size = new System.Drawing.Size(99, 26);

InitializeComponent();
this.TabPageContextMenuStrip = contextMenuStrip1;
}


private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.关闭ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.除此之外全部关闭ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.全部关闭ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.关闭ToolStripMenuItem,
this.除此之外全部关闭ToolStripMenuItem,
this.全部关闭ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.ShowImageMargin = false;
this.contextMenuStrip1.Size = new System.Drawing.Size(142, 92);
//
// 关闭ToolStripMenuItem
//
this.关闭ToolStripMenuItem.Name = "关闭ToolStripMenuItem";
this.关闭ToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.关闭ToolStripMenuItem.Text = "关闭";
this.关闭ToolStripMenuItem.Click += new System.EventHandler(this.关闭ToolStripMenuItem_Click);
//
// 除此之外全部关闭ToolStripMenuItem
//
this.除此之外全部关闭ToolStripMenuItem.Name = "除此之外全部关闭ToolStripMenuItem";
this.除此之外全部关闭ToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.除此之外全部关闭ToolStripMenuItem.Text = "除此之外全部关闭";
this.除此之外全部关闭ToolStripMenuItem.Click += new System.EventHandler(this.除此之外全部关闭ToolStripMenuItem_Click);
//
// 全部关闭ToolStripMenuItem
//
this.全部关闭ToolStripMenuItem.Name = "全部关闭ToolStripMenuItem";
this.全部关闭ToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.全部关闭ToolStripMenuItem.Text = "全部关闭";
this.全部关闭ToolStripMenuItem.Click += new System.EventHandler(this.全部关闭ToolStripMenuItem_Click);
//
// DockContentEx
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Name = "DockContentEx";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);

}

private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void 除此之外全部关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
IDockContent[] documents = DockPanel.DocumentsToArray();

foreach (IDockContent content in documents)
{
if (!content.Equals(this))
{
content.DockHandler.Close();
}
}
}

private void 全部关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
IDockContent[] documents = DockPanel.DocumentsToArray();

foreach (IDockContent content in documents)
{
content.DockHandler.Close();
}
}
}
}

其他准备以dockpanel方式打开的form都改为继承自上面这个DockPanelEx类

posted on 2012-01-17 10:15  s1ihome  阅读(2144)  评论(0编辑  收藏  举报

导航