I found many articles here on The Code Project and elsewhere that deal with
Windows.Forms dropping shadows. Most of them solved the problem by implementing a second "shadowing" Form behind the real Form. This might be useful in some cases, as this approach keeps absolute control over the to-be-drawn shadow. In other cases (e.g. customized Tooltips) it seems to involve too much overhead. Fortunately, there is a simple solution if you can live with handing control over to Windows.
I have to admit that the solution described can be found elsewhere, e.g. Windows Forms Drop Shadow. The subject of this article is not actually enough to warrant a stand-alone piece, but it might be helpful for some in this community. So, I have decided to publish this small piece of code as an article.
What It Looks Like
Note: This is a "real" shadow, since it overlays the underlying text with some opacity. It's the shadow style of
Menus, which are created in the same way.
This effect works with shaped Forms, as well. The shadow outlines the Form's shape exactly.
The "trick" is to add the
CS_DROPSHADOW value to the
ClassStyle property of the Form's
public class ShadowForm : Form
private const int CS_DROPSHADOW = 0x00020000;
protected override CreateParams CreateParams
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
Public Class ShadowForm
Private Const CS_DROPSHADOW As Integer = 131072
Protected Overrides ReadOnly Property CreateParams()
Dim cp As CreateParams = MyBase.CreateParams
cp.ClassStyle = cp.ClassStyle Or CS_DROPSHADOW
That's all you have to do.
For the described solution, Windows XP or above is required. Adding
CS_DROPSHADOW may result in an exception if executed under a lower Windows version. You can use the Environment.OSVersion property to determine the running OS version (Angelo Cresta gave a VB.NET example in a message of this article). Further on determining the actual OS is described in many CodeProject articles (e.g. XWinVer - Simple Class to get Windows OS Version, OS Name, Version & Product Type, Easily Get and Compare OS Version Information).
The thread "Moving Form bug?" in the messages of this article uncovered a strange behaviour. If the application has more than one open Window and you move the shadowed Form or it looses the focus, the shadow disappears. If the application is not maximized and you move the Forms, you will see that the shadow is still present. But it is positioned behind all Windows of the application. I guess this behaviour is connected to the way menus react: open menus (and their shadows) disappear if another object of the application is activated.
Due to this behaviour, the solution described in this article has limited use.
Points of Interest
As mentioned above, this is the way in which
Menus are created. However, Windows allows you to disable
Menu shadows. If this feature is disabled, your Form will be painted without a shadow.
At this point, you are losing control over whether your shadows will be painted or not. On the other hand, if the user dislikes shadows, why should you try to override his preferences?
- 19-06-2007: First published
- 22-06-2007: Article updated (OS version)
- 10-12-2007: Article updated (disappearing shadows)