Visual C #. NET environment programming to implement floating toolbars

Source: Internet
Author: User
The ToolStrip and ToolStripPanel controls provided in the DOTNET2.0 Development framework facilitate the development of Windows applications with dockable toolbar functionality, ToolStrip objects can be dragged and pulled across ToolStripPanel. But if you want to implement a toolbar that floats like the VS IDE or in office, you must use some third-party controls, such as devexpress, or write certain code. Here is an easy way to do this by simply inheriting the ToolStrip class.

Placed on the ToolStripPanel, when the toolbar floats, it actually changes the container object it is in, moving from its toolstrippanel to a floating container, so the following two questions need to be solved to implement the float of the toolbar:

You must have a floating container to host the ToolStrip object.
Notice when the ToolStrip object changes its container, docking between the floating container and the ToolStripPanel on the main window.

For the first question, our solution is to dynamically create a form class as a floating container named Toolstripfloatwindow, which has the following properties:

FormBorderStyle = Fixedtoolwindow Border Style
ShowInTaskbar = False does not appear in taskbar
Showicon = False does not show window icon
Topmost = true above all windows

To address the second issue, we looked at MSDN to learn that the EndDrag event is triggered when you release the mouse with the mouse dragging the ToolStrip object. In the process of this event, we determine that when the position of the ToolStrip object is moved outside the ToolStripPanel, the Toolstripfloatwindow object is created, And move the ToolStrip object to the Toolstripfloatwindow; To restore the ToolStrip object to the original form, just to determine if the Toolstripfloatwindow object's position is moved to the ToolStripPanel , moves the ToolStrip object back to ToolStripPanel and destroys the Toolstripfloatwindow object when the condition is met.

Also, to resolve that when a ToolStrip object is placed on a Toolstripfloatwindow object, the Toolstripfloatwindow object must be the same size as the ToolStrip object. There are also toolstripfloatwindow objects that are clicked on when the Close button cannot be turned off. We can do two classes to achieve the above ideas.

The Toolstripfloatwindow class inherits from the form class.
Mytoolstrip inherits from the ToolStrip class. The corresponding properties and methods are added.

The source code for the Mytoolstrip class is as follows:

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);
}

protected override void OnPaint (PaintEventArgs pe)
... {
TODO: Add custom drawing code here

Call base class OnPaint
Base. OnPaint (PE);
}

Floating state #region Floating state

Private Toolstripfloatwindow Floatwindow;

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

Floating realization of #region floating

private void Floatwindow_locationchanged (object sender, EventArgs e)
... {
When the position of the FLOATWINDWS is moved to the ToolStripPanel, the This is placed on the 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);
}

if (Currentpt.x > MINPT. X) && (Currentpt.x < MAXPT. X) && (Currentpt.y > MINPT. Y) && (Currentpt.y < MAXPT. Y))
... {
This.floatWindow.Controls.Remove (this);
This.tsPanel.SuspendLayout ();
THIS.TSPANEL.CONTROLS.ADD (this);
This. Location = This.tsPanel.PointToClient (CURRENTPT);
This.tsPanel.ResumeLayout ();
This.floatWindow.Dispose ();
This.floatwindow = null;

}
}

private void Mytoolstrip_enddrag (object sender, EventArgs e)
... {
When you decide to move out
if (This.tspanel = null)
... {
MessageBox.Show ("Please set ToolStripPanel properties First");
Return
}
Point endPoint = cursor.position;
int OpenX, Openy;
OpenX = Endpoint.x;
Openy = Endpoint.y;
Point clientpt = This.tsPanel.Parent.PointToClient (endPoint);
if (Clientpt.y > Tspanel.height)
... {
Toolstripfloatwindow FW = new Toolstripfloatwindow ();
This.tsPanel.Controls.Remove (this);
Fw. Controls.Add (this);
This. left = 0;
This. top = 0;
This. Floatwindow = FW;
Point newloc = new Point (OpenX, Openy);
Fw. Show ();
Fw. Location = Newloc;
Fw. SetBounds (newloc.x, Newloc.y, this.) Clientsize.width, this. Clientsize.height);
}
}

private void Floatwindow_formclosing (object sender, FormClosingEventArgs e)
... {
E.cancel = true;
}

private void Mytoolstrip_sizechanged (object sender, EventArgs e)
... {
if (this.isfloating)
... {
This.floatWindow.Width = this. Clientsize.width;
}
}

#endregion

}
}

Conclusion. This method is easy to implement, when it is not willing to use a powerful Third-party control library can be used this method, the disadvantage is that the container is responsible for floating is a window, not beautiful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.