C ToolStrip makes a four sided dock floating toolbar
- 2020-05-26 10:01:48
- OfStack
About how to make floating toolbar, the jaguar wrote a very good article, see: https: / / www ofstack. com article / 44272. htm
This toolbar can only dock at the top after floating. Based on this, I will dock at the left/right/bottom here. The dock condition is that the floating form is close to or over the edge of the main form.
Actually, the code given by archer is quite detailed :) I mainly present the rewriting ToolStrip code segment here, adding 3 ToolStripPanel
public partial class MyToolStrip : ToolStrip
{
public MyToolStrip()
{
InitializeComponent();
this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
}
#region Floating state
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 Floating implementation
private void FloatWindow_LocationChanged(object sender, EventArgs e)
{
// when floatwindws The position is moved to toolstrippanel When will this Placed in toolstripPanel on
if (this.FloatWindow == null)
{
return;
}
if (FloatWindow.HasCreated)
{
// Main form location
Point frmLoc = this.TopToolStripPanel.Parent.Location;
// Float the toolbar position
Point toolBarLoc = FloatWindow.Location;
if (toolBarLoc.Y - frmLoc.Y <= 0) // At the top 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) // On the left 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) // On the right side 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) // At the bottom 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);
// The floating area
Rectangle floatArea = new Rectangle(32, 32, // I'm going to resize the icon here 32*32
this.TopToolStripPanel.Parent.Width - 2 * 32,
this.TopToolStripPanel.Parent.Height - 2 * 32);
if (floatArea.Contains(clientPt)) // Judge when to move out
{
ToolStripFloatWindow fw = new ToolStripFloatWindow();
fw.Controls.Add(this);
this.Left = 0;
this.Top = 0;
this.FloatWindow = fw;
FloatWindow.LocationChanged += new EventHandler(FloatWindow_LocationChanged);
fw.SetBounds(screenPt.X, screenPt.Y, this.ClientSize.Width, this.ClientSize.Height + 22); //22 Is the height of the form title bar
fw.Show();
}
}
private void MyToolStrip_SizeChanged(object sender, EventArgs e)
{
if (this.isFloating)
{
this.FloatWindow.Width = this.ClientSize.Width;
}
}
#endregion
}