WeifenLuo.WinFormsUI.Docking添加关闭功能

/******************************************************************
* 创建人:HTL
* 创建时间:2014-7-8 15:37:20
* 说明:WinFormsUI添加右键关闭菜单
* Email:huangyuan413026@163.com
*******************************************************************/
usingSystem;
usingWeifenLuo.WinFormsUI.Docking;
namespace WinForm.BaseForms
{
/// <summary>
/// WinFormsUI添加右键关闭菜单
/// 让其它窗体只要继承这个就有了这个右键菜单,即可关闭窗体
/// </summary>
publicclassDockContentEx:DockContent
{
//在Tag列表上添加右键关闭菜单
publicDockContentEx()
{
System.Windows.Forms.ContextMenuStrip cms =newSystem.Windows.Forms.ContextMenuStrip();
cms.Items.AddRange(newSystem.Windows.Forms.ToolStripItem[]{
newSystem.Windows.Forms.ToolStripMenuItem("关闭", null, tsmiClose_Click,"tsmiClose")
,newSystem.Windows.Forms.ToolStripMenuItem("关闭其他", null, tsmiClose_Click,"tsmiOtherClose")
,newSystem.Windows.Forms.ToolStripMenuItem("全部关闭", null, tsmiClose_Click,"tsmiAllClose")
});
//将右键菜单绑定到DockContent的TabPage上
this.TabPageContextMenuStrip= cms;
//也可以使用这种方式
//this.TabPageContextMenu=newSystem.Windows.Forms.ContextMenu();
}
//菜单事件
privatevoid tsmiClose_Click(object sender,EventArgs e)
{
System.Windows.Forms.ToolStripMenuItem _tsmt = sender as System.Windows.Forms.ToolStripMenuItem;
if(_tsmt == null)return;
switch(_tsmt.Name)
{
case"tsmiClose":this.Close();break;
case"tsmiAllClose":HideOrCloseFrom(true);break;
case"tsmiOtherClose":HideOrCloseFrom();break;
default:break;
}
}
/// <summary>
/// 隐藏其他窗体
/// </summary>
voidHideOrCloseFrom()
{
HideOrCloseFrom(false,false);
}
/// <summary>
/// 隐藏窗体
/// </summary>
/// <param name="isAll">隐藏所有还是其他窗体</param>
voidHideOrCloseFrom(bool isAll)
{
HideOrCloseFrom(isAll,false);
}
/// <summary>
/// 关闭还是隐藏窗体
/// </summary>
/// <param name="isAll">是否是所有窗体,还是除当前窗体以外的其他窗体</param>
/// <param name="isClose">是否是关闭窗体,默认为隐藏窗体</param>
privatevoidHideOrCloseFrom(bool isAll,bool isClose)
{
DockContentCollection contents =DockPanel.Contents;
for(int i =0; i < contents.Count; i++)
{
if(isAll)
{
HideOrCloseFrom(contents[i].DockHandler, isClose);
}//end if
elseif(DockPanel.ActiveContent!= contents[i])
{
HideOrCloseFrom(contents[i].DockHandler, isClose);
}//END ELSE IF
}//end for
}//end HideOrCloseFrom
/// <summary>
/// 关闭或隐藏当前窗体
/// 仅DockState状态跟当前的DockState状态相同的窗体
/// </summary>
/// <param name="content">当前窗体对象</param>
voidHideOrCloseFrom(DockContentHandler content,bool isClose)
{
if(content == null || content.DockState!=this.DockState)return;
if(isClose)
content.Close();
elseif(!content.IsHidden)
content.Hide();
}
}
}

  

主要是利用WinFormsUI的TabPageContextMenuStrip属性或TabPageContextMenu绑定自定义的右键菜单来实现的
//将右键菜单绑定到DockContent的TabPage上
this.TabPageContextMenuStrip= cms;
//也可以使用这种方式
//this.TabPageContextMenu = new System.Windows.Forms.ContextMenu();

  

参考:

http://www.cnblogs.com/luomingui/p/3329763.html

WinFormsUI开源地址:https://github.com/dockpanelsuite/dockpanelsuite

http://blog.yotuo.net/post/2010/09/WeifenLuo.WinFormsUI.Docking_doubleclick_close.html

MSDN MenuItem 类:http://msdn.microsoft.com/zh-cn/library/System.Windows.Forms.MenuItem(v=vs.80).aspx

MSDN ToolStripMenuItem 类:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.toolstripmenuitem(v=vs.80).aspx

WeifenLuo.WinFormsUI.Docking添加关闭功能,布布扣,bubuko.com

WeifenLuo.WinFormsUI.Docking添加关闭功能

上一篇:当程序以Windows Services形式启动时当前路径不对


下一篇:WPF - ViewModle中关闭Window