Adding a drop shadow to a windows form

February 13th, 2007 by Chris

This nice tip allows the addition of a drop shadow to any form you want. The drop shadow is a nice effect that comes standard with Windows XP and Vista. It just gives the form a little mode depth and a nicer more modern look.

As you can see above, the difference is slight, but in the right hand image you can see a drop shadow around the form.

To implement this feature, you need to add in the following code to your forms.


C#:
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Security;
  4. using System.Security.Permissions;
  5.  
  6. namespace System.Windows.Forms
  7. {
  8.     public class DropShadowForm : Form
  9.     {
  10.         private const int CS_DROPSHADOW = 0x00020000;
  11.  
  12.         public DropShadowForm()
  13.         {
  14.         }
  15.  
  16.         protected override CreateParams CreateParams
  17.         {
  18.             [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  19.             get
  20.             {
  21.                 CreateParams parameters = base.CreateParams;
  22.  
  23.                 if (DropShadowSupported)
  24.                 {
  25.                     parameters.ClassStyle = (parameters.ClassStyle | CS_DROPSHADOW);
  26.                 }
  27.                 return parameters;
  28.             }
  29.         }
  30.  
  31.         public static bool DropShadowSupported
  32.         {
  33.             get { return IsWindowsXPOrAbove; }
  34.         }
  35.  
  36.         public static bool IsWindowsXPOrAbove
  37.         {
  38.             get
  39.             {
  40.                 OperatingSystem system = Environment.OSVersion;
  41.                 bool runningNT = system.Platform == PlatformID.Win32NT;
  42.  
  43.                 return runningNT && system.Version.CompareTo(new Version(5, 1, 0, 0))>= 0;
  44.             }
  45.         }
  46.     }
  47. }

Posted in Windows Forms | 1 Comment »

New Site

February 13th, 2007 by Chris

Welcome to my new .NET tips and tricks site, my focus of this site is to provide a wealth of information in relation to .NET development, deployment and use as well as some of the more trickier side of the .NET world.



Posted in Site | No Comments »