Floating, collapsible transparent window in C#






3.29/5 (16 votes)
Mar 4, 2005
1 min read

103161

3871
How to create a floating, collapsible transparent form in C# using the NativeWindow class
Introduction
Working with Windows Forms is wonderful and all the features offered by the framework and Visual Studio make working so enjoyable. But every now and then it comes a time in the life of a programmer when he needs a challenge. This happened to me a few months ago when I was working on some user control and I thought it would be really nice if at design-time I would be able to present the user with a context menu much more appropiate then the one included in Visual Studio. So, the idea of a window that can be popped up on the screen emerged and this is how the floating native window came to be.
Using the code
The code is very straightforward. There is a FloatingWindow
class declared inside the Olvio.FloatingWindow.dll library and
the solution also includes a Test that demonstrates the tool.
This is the code used to show the window:
private void button1_Click(object sender, System.EventArgs e)
{
Olvio.Windows.Forms.AnimateMode anim = (Olvio.Windows.Forms.AnimateMode)
Enum.Parse(typeof(Olvio.Windows.Forms.AnimateMode),
this.comboBox1.Text, true);
fw.ShowAnimate(fw.Location.X, fw.Location.Y, anim);
}
As you can see the FloatingWindow can be shown with one of the 10 animation modes:
public enum AnimateMode
{
SlideRightToLeft,
SlideLeftToRight,
SlideTopToBottom,
SlideBottmToTop,
RollRightToLeft,
RollLeftToRight,
RollTopToBottom, RollBottmToTop,
Blend,
ExpandCollapse
}
The test project actually creates an inherited FloatingWindow to demonstrate
the ability to do custom drawing by overriding
the PerformPaint
method.
Also, the demo shows how you can modify the alpha-blend propertie at run-time. For this purpose the test project displays a NumericUpDown control.
The window also displays a caption bar, a semi-transparent shadow, a minimize/maximize button and a close button. Also, the window can be resized using the resize grip in the bottom-right corner.
History
Version 1.0 - First version.
Known bugs:
- Hiding with animation produces a strange flicker