DotNet2.0开发框架中提供的ToolStrip和ToolStripPanel控件可以方便开发具有可停靠工具栏功能的Windows应用程序, ToolStrip对象可以在各个ToolStripPanel间完成拖拽停靠,但是如果想实现类似VS IDE 或Office中可以浮动的工具栏必须借助于DevExpress等一些第三方的控件或编写一定的代码。 这里介绍一种比较简单的方法,只需继承ToolStrip类即可实现上述的效果。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;
namespace FloatingToolStrip ...{ public partial class MyToolStrip : ToolStrip ...{ public MyToolStrip() ...{ InitializeComponent(); this.EndDrag += new EventHandler(MyToolStrip_EndDrag); this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged); }
public ToolStripFloatWindow FloatWindow ...{ get ...{ return this.floatWindow; } set ...{ floatWindow = value; if (FloatWindow != null) ...{ floatWindow.LocationChanged += new EventHandler(floatWindow_LocationChanged); floatWindow.FormClosing += new FormClosingEventHandler(floatWindow_FormClosing); } } }
public bool isFloating ...{ get ...{ return (floatWindow != null); } }
private ToolStripPanel tsPanel;
public ToolStripPanel ToolStripPanel ...{ get ...{ return this.tsPanel; } set ...{ tsPanel = value; } }
#endregion
漂浮实现#region 漂浮实现
private void floatWindow_LocationChanged(object sender, EventArgs e) ...{ //当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上 if (this.floatWindow == null) ...{ return; } Point currentPt = new Point(floatWindow.Location.X, floatWindow.Location.Y); Point minpt = this.tsPanel.PointToScreen(tsPanel.Location); Point maxpt; if(this.tsPanel.Height <= 20)...{ maxpt = new Point(minpt.X + this.tsPanel.Width, minpt.Y + 20); }else...{ maxpt = new Point(minpt.X + this.tsPanel.Width, minpt.Y + this.tsPanel.Height); }
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved 京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089