Pobierz kod źródłowy, skompilowaną bibliotekę oraz przykładowe aplikacje (v1.3.1, VS 2008) — 78 KB
Pobierz kod źródłowy, skompilowaną bibliotekę oraz przykładowe aplikacje (v1.2.5, VS 2005) — 86.8 KB
Zobacz pierwotną wersję artykułu na stronie The Code Project
Treść artykułu jest dostępna jedynie w języku angielskim.
Introduction
Pop-up windows are everywhere. Each tooltip is a pop-up window; each combobox has its pop-up list; many advertisements are also shown in pop-up windows.
How To Create a Pop-up Control in .NET?
At first, we might choose the Form class as a base class for our pop-up control. Unfortunately, it is a bad choice because when we show our pop-up form, the parent form loses its focus. A pop-up window should not cause that. Luckily, there is a class that does not cause loss of focus. We can use it as a base class for our pop-up control. It is the ToolStripDropDown class.
How To Use the ToolStripDropDown Class?
This would be the simplest way, without deriving from that class:
ToolStripDropDown popup = new ToolStripDropDown();
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(content);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(parentForm, location);
In this case, content is a control we want to show in a pop-up window. Of course, we have to remember to later dispose the pop-up window and its contents.
Popup Class
I wrote a Popup class that derives from ToolStripDropDown and simplifies the creating and managing of pop-up windows. The class calculates by itself where it should “pop” on the screen. It also disposes itself immediately after disposing the content control. To show a pop-up with a button, for example, we could write:
new Popup(new Button()).Show(someControl);
Here, someControl would be a control below which we want to show our pop-up.
Pop-up Resizing
To enable resizing for your pop-up, you must set the Resizable property to true and add the following code into your content control class:
protected override void WndProc(ref Message m)
{
if ((Parent as Popup).ProcessResizing(ref m)) return;
base.WndProc(ref m);
}
You also have to set the ResizeRedraw property of the content control to true.
Important!
To specify the minimum and maximum size of the content control, please use the following properties:
content.MinimumSize and content.MaximumSize only inside the constructor of the content control
popup.MinimumSize and popup.MaximumSize elsewhere
ComboBox Class
The System.Windows.Forms.ComboBox class behaves in a strange way when it is on a pop-up control. It closes the pop-up control when the user clicks on a part of the combobox’s dropdown that sticks out of a pop-up. So, I have created a PopupControl.ComboBox class that behaves properly.
PopupComboBox Class
This is a base class for comboboxes that can have a custom dropdown attached.
Animation Support
Animation is enabled by default. To change it, set AnimationDuration, HidingAnimation and ShowingAnimation properties.
Popup Members
Properties
AcceptAlt — Gets or sets a value indicating whether pressing the Alt key should close the pop-up.
AnimationDuration — Determines the duration of the animation.
Content — Gets the content of the pop-up.
FocusOnOpen — Gets or sets a value indicating whether the content should receive the focus after the pop-up has been opened.
HidingAnimation — Determines which animation to use while hiding the pop-up window.
MaximumSize — Gets or sets a maximum size of the pop-up.
MinimumSize — Gets or sets a minimum size of the pop-up.
Resizable — Gets or sets a value indicating whether the pop-up is resizable.
ShowingAnimation — Determines which animation to use while showing the pop-up window.
Constructor
Popup(Control content) — Initializes a new instance of the Popup class.
Methods
void PaintSizeGrip(PaintEventArgs e) — Paints the sizing grip.
bool ProcessResizing(ref Message m) — Processes the resizing messages.
void Show(Control control) — Shows the pop-up window below the specified control.
void Show(Control control, Rectangle area) — Shows the pop-up window below the specified area of the specified control.
Screenshots
Sample application using a custom pop-up control:
... and a more complex pop-up that can be resized:
Another application using a custom tooltip...
Cascade of pop-up controls:
Tracking pop-up opening below the specified part of a control:
TODO
- Base class for custom tooltips.
- Office 2007-like tooltip class.
History
- 1.3.1 (20.09.2008)
- Tab-key properly transfers the focus between controls contained in a pop-up window.
- Minor bugs fixed.
- 1.3 (04.05.2008)
- Added
AnimationDuration, HidingAnimation and ShowingAnimation properties.
- Removed
UseFadeEffect property.
- Pop-up control can animate now even when the
FocusOnOpen property is set to false.
- 1.2.5 (24.01.2008) – This is the last available version for Visual Studio 2005.
- Fixed “Alt+F4” bug.
- Fixed drawing the sizing grip.
- Minor bugs fixed.
- 1.2 (24.07.2007)
- Added animation support.
- Added
AcceptAlt property.
- Clicking on the non-client area bug fixed.
- Minor bugs fixed.
- 1.1 (05.07.2007)
- Added XML documentation.
- “Fixed” the
ComboBox class, so it can be used inside a pop-up.
- Added base class for a combobox that can have a custom dropdown,
PopupComboBox.
- The sizing grip is automatically drawn if a pop-up is resizable.
- Added support for a minimum and maximum size of a resizable pop-up.
- Minor bugs fixed.
- 1.0
- 08.06.2007 – Added resizing support and capability for using cascading pop-ups.
- 06.02.2007 – Added keyboard and custom region support, the
ShowForControl method name changed to Show.
- 03.02.2007 – First version.
License
This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL).