Floating toolbar for programming in Visual C #. Net Environment

Source: Internet
Author: User
The toolstrip and toolstrippanel controls provided in the dotnet2.0 development framework facilitate the development of Windows applications with the tool bar function. toolstrip objects can be dragged and docked between various toolstrippanels, however, if you want to implement a toolbar similar to vs ide or office that can be floating, you must use third-party controls such as devexpress or write some code. This article introduces a simple method. You only need to inherit the toolstrip class to achieve the above effect.

Placed on the toolstrippanel, when the toolbar is floating, it actually changes its container object and moves from its toolstrippanel to a floating container, therefore, to float the toolbar, you must solve the following two problems:

There must be a floating container to carry the toolstrip object.
You must know when the toolstrip object changes its container, that is, it is docked between the floating container and the toolstrippanel on the main window.

For the first problem, our solution is to dynamically create a form class as a floating container named toolstripfloatwindow. This form object has the following attributes:

Formborderstyle = fixedtoolwindow border Style
Showintaskbar = false is not displayed in the taskbar
Showicon = false do not display the window icon
Topmost = true on all windows

To solve the second problem, we can refer to msdn to learn that the enddrag event is triggered when you drag the toolstrip object to release the mouse. In the event processing method, we determine that when the toolstrip object location is moved outside the toolstrippanel, create the toolstripfloatwindow object and move the toolstrip object to the toolstripfloatwindow; to restore a toolstrip object to the original form, you only need to determine whether the position of the toolstripfloatwindow object has been moved to the toolstrippanel. when the conditions are met, move the toolstrip object back to the toolstrippanel and destroy the toolstripfloatwindow object.

In addition, when the toolstrip object is placed on the toolstripfloatwindow object, the toolstripfloatwindow object must be of the same size as the toolstrip object. You cannot close a toolstripfloatwindow object when the close button is clicked. We can do two classes to implement the above ideas.

The toolstripfloatwindow class inherits from the form class.
Mytoolstrip inherits from the toolstrip class. Added the corresponding attributes and methods.

The source code of 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 draw code here

// Call the base class onpaint
Base. onpaint (PE );
}

Floating status # region floating status

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
...{
Get
...{
Return this. tspanel;
}
Set
...{
Tspanel = value;
}
}

# Endregion

Floating implementation # region floating implementation

Private void floatwindow_locationchanged (Object sender, eventargs E)
...{
// When floatwindws is moved to the toolstrippanel, place this 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 judging the removal
If (this. tspanel = NULL)
...{
MessageBox. Show ("set the toolstrippanel attribute 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

}
}

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.