Click here to Skip to main content
Click here to Skip to main content

Let Your Form Drop a Shadow

By , 15 Dec 2007
 

Introduction

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

Screenshot - shadowform.gif

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 Code

The "trick" is to add the CS_DROPSHADOW value to the ClassStyle property of the Form's CreateParams property.

C#

public class ShadowForm : Form
{
    // Define the CS_DROPSHADOW constant
    private const int CS_DROPSHADOW = 0x00020000;

    // Override the CreateParams property
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ClassStyle |= CS_DROPSHADOW;
            return cp;
        }
    }
}

Visual Basic

Public Class ShadowForm

  ' Define the CS_DROPSHADOW constant
  Private Const CS_DROPSHADOW As Integer = 131072

  ' Override the CreateParams property
  Protected Overrides ReadOnly Property CreateParams()
      As System.Windows.Forms.CreateParams

    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ClassStyle = cp.ClassStyle Or CS_DROPSHADOW
        Return cp
    End Get

  End Property

End Class

That's all you have to do.

Limitation

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.

Screenshot - display_properties.gif

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?

History

  • 19-06-2007: First published
  • 22-06-2007: Article updated (OS version)
  • 10-12-2007: Article updated (disappearing shadows)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marcus Deecke
Software Developer (Senior)
Germany Germany
I am working for a small software company in Hamburg, Germany, and my day-to-day distress is Java development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDoubt about the corners PinmemberJeffrey Matheus3-Feb-13 9:29 
Is there any way to make it larger?
I'm using W7 and wanted to make in all borders.

GeneralI didn't understand the limitations of this approach. PinmemberM N Sarofem29-Jul-12 0:14 
Please, i need to make form's shadow alive every time the form has a focus. thanks in advance
QuestionProlem with MDI form Pinmembershriram201219-Apr-12 16:00 
the above code is totaly fine but not with MDI's child form so please give solution to drop shadow for MDI child form
GeneralDie Schatten in Vista oder Win7 sind anders... Pinmemberdherrmann5-Dec-09 6:27 
Hallo Marcus,
 
du hast sicher schon die shadows bei den Fenstern unter Vista oder Win7 gesehen. Die sehen wesentlich anders aus, sind auch breiter...
Hast du ne Idee, wie man das für borderless windows erreichen kann?
 
Gruß-
Dietrich
 
[Hello Marcus,
certainly you have seen shadows of windows of Vista or Win7. They look significant different, they are wider too...
Do you have an idea, how to acquire this effect for borderless windows? ]
GeneralThat was great! Pinmemberk0zaw9-Jun-09 19:19 
Thanks for sharing.
 
I've found the right one for my app.
 
But I notice it wasn't work for non-rectangular form (with transparency).
 
Is there any solution?
GeneralRe: That was great! PinmemberMarcus Deecke16-Oct-09 11:01 
GeneralRe: That was great! PinmemberGee.4-Feb-10 6:19 
QuestionHow to set shadow for child window of the MDI form PinmemberMember 12303323-Jul-08 9:18 
Hello ,
Nice article on Shadow. How to set the shadow for all Children of the MDI form.
 
Please help
thank you
GeneralShadow tweaks Pinmemberjberenguer2-Jul-08 11:18 
Can anyone provide examples of how you would change shadow positioning and maybe color? I would like to have some forms cast farther shadows giving the impression that the window is a little closer and also would like to change orientation or position of shadow so I can make the shadow edges appear on all sides of the form - then change the color for a psuedo glow kind of look. Does anyone have any ideas on how to do this using this or another method?
GeneralRe: Shadow tweaks PinmemberPaul Sanders (AlpineSoft)7-Sep-08 7:56 
GeneralKeeping dropshadows Pinmemberjberenguer2-Jul-08 11:13 
This works very well, but for future reference - how would you stop the users from removing the drop shadow? I agree users should be able to turn this off and on, but still would like to know - Is there any code or examples anyone could give to stop windows and user from removing shadow?
Questionhow to stop the drop shadow from disappearing when you lose focus Pinmemberpaperclippy2-Apr-08 10:45 
Assuming you always want your drop-shadow form to be on top of everything else, it appears that setting your Form's .TopMost to true will stop the shadow from disappearing when you lose focus.
AnswerRe: how to stop the drop shadow from disappearing when you lose focus Pinmemberjberenguer25-Nov-08 4:51 
GeneralGood Job Pinmembermerlin98117-Dec-07 4:41 
Thanks for sharing this


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Rhabot - World of Warcraft Bot

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GeneralThanks PinmemberJaseNet26-Jun-07 6:23 
Very small and useful snippet.
Thanks to all
GeneralCheck for SystemParameter.DropShadow PinmemberLaurent Muller25-Jun-07 19:40 
You can use the OSFeature.IsPresent function with the SystemParameter.DropShadow argument.
 
const int CS_DROPSHADOW = 0x20000;
 
protected override CreateParams CreateParams
{
 get
 {
   CreateParams parameters = base.CreateParams;
   if (OSFeature.IsPresent(SystemParameter.DropShadow))
   {
    parameters.ClassStyle |= CS_DROPSHADOW;
   }
   return parameters;
 }
}

GeneralRe: Check for SystemParameter.DropShadow PinmemberJamie Nordmeyer26-Jun-07 4:54 
GeneralRe: Check for SystemParameter.DropShadow PinmemberThe_Mega_ZZTer26-Jun-07 15:48 
QuestionRe: Check for SystemParameter.DropShadow PinmemberApurva363318-Aug-11 2:42 
QuestionMoving Form bug? PinmemberSteve J Randall21-Jun-07 0:18 
When I display a form the shadowing is present, but when I move the form with the mouse to another position on the screen (and then release the mouse) the shadowing disappears? Is this a bug?
AnswerRe: Moving Form bug? PinmemberMarcus Deecke21-Jun-07 22:58 
GeneralRe: Moving Form bug? PinmemberMarcus_228-Jun-07 1:15 
GeneralRe: Moving Form bug? Pinmemberracmax3-Jul-07 5:15 
GeneralNice job PinmemberNickolay Karnaukhov20-Jun-07 23:03 
In posts above - guys has suggested that OS version needs to be checked - I think it can be a "Do Yourself" feature Smile | :) Sometimes people need to be able to fix some visible problems - that will help them to understand a code Smile | :)
 
Nice job, nice artice, 5+
 
------------------------------------------------------------
Want to be happy - do what you like!

GeneralRe: Nice job PinmemberAngelo Cresta21-Jun-07 4:51 
AnswerRe: Nice job PinmemberMarcus Deecke21-Jun-07 23:03 
GeneralOS version PinmemberMuhammed Sahin20-Jun-07 19:33 
Hi
You must check OS version. XP or above required for shadow.

GeneralRe: OS version PinmemberGeorgi Atanasov20-Jun-07 21:40 
GeneralRe: OS version PinmemberMarcus Deecke21-Jun-07 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 16 Dec 2007
Article Copyright 2007 by Marcus Deecke
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid