zoukankan      html  css  js  c++  java
  • 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类

  • 相关阅读:
    第11条:用zip函数同时遍历两个迭代器
    第10条:尽量用enumerate取代range
    第9条:用生成器表达式来改写数据量较大的列表推导式
    MySQL的约束
    VMware下所有的系统网卡启动不起来
    windows下的mysql闪退问题
    大型网站架构模式
    MySQL的information_schema库
    mysql复制表结构和内容
    希尔排序 堆排序 归并排序
  • 原文地址:https://www.cnblogs.com/s1ihome/p/2324369.html
Copyright © 2011-2022 走看看