关于浮动工具条的制作,阿捷写了一篇很不错的文章,见:http://www.cnblogs.com/ajiefj/archive/2010/04/27/1722256.html
阿捷这个工具条浮动后只能在顶部停靠,基于此,我在这边增加在左/右/底部停靠,停靠条件是浮动窗体紧贴或越过主窗体边缘。

其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的ToolStrip代码段,增加了三个ToolStripPanel

  1. public partial class MyToolStrip : ToolStrip  
  2. {  
  3.     public MyToolStrip()  
  4.     {  
  5.         InitializeComponent();  
  6.         this.EndDrag += new EventHandler(MyToolStrip_EndDrag);  
  7.         this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);  
  8.     }  
  9.  
  10.     #region 漂浮状态  
  11.   
  12.     public ToolStripFloatWindow FloatWindow { getset; }  
  13.   
  14.     private bool isFloating  
  15.     {  
  16.         get  
  17.         {  
  18.             return (FloatWindow != null);  
  19.         }  
  20.     }  
  21.   
  22.     public ToolStripPanel TopToolStripPanel { getset; }  
  23.     public ToolStripPanel BottomToolStripPanel { getset; }  
  24.     public ToolStripPanel LeftToolStripPanel { getset; }  
  25.     public ToolStripPanel RightToolStripPanel { getset; }  
  26.  
  27.     #endregion  
  28.  
  29.     #region 漂浮实现  
  30.   
  31.     private void FloatWindow_LocationChanged(object sender, EventArgs e)  
  32.     {  
  33.         //当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上  
  34.         if (this.FloatWindow == null)  
  35.         {  
  36.             return;  
  37.         }  
  38.         if (FloatWindow.HasCreated)  
  39.         {  
  40.             //主窗体位置  
  41.             Point frmLoc = this.TopToolStripPanel.Parent.Location;  
  42.             //浮动工具条位置  
  43.             Point toolBarLoc = FloatWindow.Location;  
  44.   
  45.             if (toolBarLoc.Y - frmLoc.Y <= 0) //置于顶部StripPanel  
  46.             {  
  47.                 this.FloatWindow.Controls.Remove(this);  
  48.                 this.TopToolStripPanel.SuspendLayout();  
  49.                 this.TopToolStripPanel.Controls.Add(this);  
  50.                 this.Location = this.TopToolStripPanel.PointToClient(toolBarLoc);  
  51.                 this.TopToolStripPanel.ResumeLayout();  
  52.                 this.FloatWindow.Dispose();  
  53.                 this.FloatWindow = null;  
  54.                 return;  
  55.             }  
  56.             if (toolBarLoc.X - frmLoc.X <= 0) //置于左边StripPanel  
  57.             {  
  58.                 this.FloatWindow.Controls.Remove(this);  
  59.                 this.LeftToolStripPanel.SuspendLayout();  
  60.                 this.LeftToolStripPanel.Controls.Add(this);  
  61.                 this.Location = this.LeftToolStripPanel.PointToClient(toolBarLoc);  
  62.                 this.LeftToolStripPanel.ResumeLayout();  
  63.                 this.FloatWindow.Dispose();  
  64.                 this.FloatWindow = null;  
  65.                 return;  
  66.             }  
  67.             if (toolBarLoc.X + FloatWindow.Width >= this.TopToolStripPanel.Parent.Width) //置于右边StripPanel  
  68.             {  
  69.                 this.FloatWindow.Controls.Remove(this);  
  70.                 this.RightToolStripPanel.SuspendLayout();  
  71.                 this.RightToolStripPanel.Controls.Add(this);  
  72.                 this.Location = this.RightToolStripPanel.PointToClient(toolBarLoc);  
  73.                 this.RightToolStripPanel.ResumeLayout();  
  74.                 this.FloatWindow.Dispose();  
  75.                 this.FloatWindow = null;  
  76.                 return;  
  77.             }  
  78.             if (toolBarLoc.Y + FloatWindow.Height >= this.TopToolStripPanel.Parent.Height) //置于底部StripPanel  
  79.             {  
  80.                 this.FloatWindow.Controls.Remove(this);  
  81.                 this.BottomToolStripPanel.SuspendLayout();  
  82.                 this.BottomToolStripPanel.Controls.Add(this);  
  83.                 this.Location = this.BottomToolStripPanel.PointToClient(toolBarLoc);  
  84.                 this.BottomToolStripPanel.ResumeLayout();  
  85.                 this.FloatWindow.Dispose();  
  86.                 this.FloatWindow = null;  
  87.                 return;  
  88.             }  
  89.         }  
  90.     }  
  91.   
  92.     private void MyToolStrip_EndDrag(object sender, EventArgs e)  
  93.     {  
  94.         Point screenPt = Cursor.Position;  
  95.         Point clientPt = this.TopToolStripPanel.Parent.PointToClient(screenPt);  
  96.   
  97.         //浮动区域  
  98.         Rectangle floatArea = new Rectangle(32, 32,    //我这里图标大小调整为32*32  
  99.             this.TopToolStripPanel.Parent.Width - 2 * 32,   
  100.             this.TopToolStripPanel.Parent.Height - 2 * 32);  
  101.   
  102.         if (floatArea.Contains(clientPt)) //判断移出时  
  103.         {  
  104.             ToolStripFloatWindow fw = new ToolStripFloatWindow();  
  105.             fw.Controls.Add(this);  
  106.             this.Left = 0;  
  107.             this.Top = 0;  
  108.             this.FloatWindow = fw;  
  109.             FloatWindow.LocationChanged += new EventHandler(FloatWindow_LocationChanged);  
  110.             fw.SetBounds(screenPt.X, screenPt.Y, this.ClientSize.Width, this.ClientSize.Height + 22); //22为窗体标题栏高度  
  111.               fw.Show();  
  112.          }  
  113.     }  
  114.   
  115.     private void MyToolStrip_SizeChanged(object sender, EventArgs e)  
  116.     {  
  117.         if (this.isFloating)  
  118.         {  
  119.             this.FloatWindow.Width = this.ClientSize.Width;  
  120.         }  
  121.     }  
  122.  
  123.     #endregion  
  124.   
  125. }  
posted on 2012-06-15 23:23  Peequeue  阅读(1284)  评论(1编辑  收藏  举报