您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

C# ToolStrip制作四边停靠浮动工具栏

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 这篇文章主要介绍了C# ToolStrip浮动工具栏的制作,可以上/下/左/右停靠,代码在下面

关于浮动工具条的制作   其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的ToolStrip代码段,增加了三个ToolStripPanel       代码如下:     public partial class MyToolStrip : ToolStrip     {         public MyToolStrip()         {             InitializeComponent();             this.EndDrag += new EventHandler(MyToolStrip_EndDrag);             this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);         }           #region 漂浮状态           public ToolStripFloatWindow FloatWindow { get; set; }           private bool isFloating         {             get             {                 return (FloatWindow != null);             }         }           public ToolStripPanel TopToolStripPanel { get; set; }         public ToolStripPanel BottomToolStripPanel { get; set; }         public ToolStripPanel LeftToolStripPanel { get; set; }         public ToolStripPanel RightToolStripPanel { get; set; }           #endregion           #region 漂浮实现           private void FloatWindow_LocationChanged(object sender, EventArgs e)         {             //当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上             if (this.FloatWindow == null)             {                 return;             }             if (FloatWindow.HasCreated)             {                 //主窗体位置                 Point frmLoc = this.TopToolStripPanel.Parent.Location;                 //浮动工具条位置                 Point toolBarLoc = FloatWindow.Location;                   if (toolBarLoc.Y - frmLoc.Y <= 0) //置于顶部StripPanel                 {                     this.FloatWindow.Controls.Remove(this);                     this.TopToolStripPanel.SuspendLayout();                     this.TopToolStripPanel.Controls.Add(this);                     this.Location = this.TopToolStripPanel.PointToClient(toolBarLoc);                     this.TopToolStripPanel.ResumeLayout();                     this.FloatWindow.Dispose();                     this.FloatWindow = null;                     return;                 }                 if (toolBarLoc.X - frmLoc.X <= 0) //置于左边StripPanel                 {                     this.FloatWindow.Controls.Remove(this);                     this.LeftToolStripPanel.SuspendLayout();                     this.LeftToolStripPanel.Controls.Add(this);                     this.Location = this.LeftToolStripPanel.PointToClient(toolBarLoc);                     this.LeftToolStripPanel.ResumeLayout();                     this.FloatWindow.Dispose();                     this.FloatWindow = null;                     return;                 }                 if (toolBarLoc.X + FloatWindow.Width >= this.TopToolStripPanel.Parent.Width) //置于右边StripPanel                 {                     this.FloatWindow.Controls.Remove(this);                     this.RightToolStripPanel.SuspendLayout();                     this.RightToolStripPanel.Controls.Add(this);                     this.Location = this.RightToolStripPanel.PointToClient(toolBarLoc);                     this.RightToolStripPanel.ResumeLayout();                     this.FloatWindow.Dispose();                     this.FloatWindow = null;                     return;                 }                 if (toolBarLoc.Y + FloatWindow.Height >= this.TopToolStripPanel.Parent.Height) //置于底部StripPanel                 {                     this.FloatWindow.Controls.Remove(this);                     this.BottomToolStripPanel.SuspendLayout();                     this.BottomToolStripPanel.Controls.Add(this);                     this.Location = this.BottomToolStripPanel.PointToClient(toolBarLoc);                     this.BottomToolStripPanel.ResumeLayout();                     this.FloatWindow.Dispose();                     this.FloatWindow = null;                     return;                 }             }         }           private void MyToolStrip_EndDrag(object sender, EventArgs e)         {             Point screenPt = Cursor.Position;             Point clientPt = this.TopToolStripPanel.Parent.PointToClient(screenPt);               //浮动区域             Rectangle floatArea = new Rectangle(32, 32,    //我这里图标大小调整为32*32                 this.TopToolStripPanel.Parent.Width - 2 * 32,                  this.TopToolStripPanel.Parent.Height - 2 * 32);               if (floatArea.Contains(clientPt)) //判断移出时   &nb