Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Shadow On A Borderless WinForm

Tags:

I'm trying to drop a shadow around the whole form just like the first picture, except that that is a WPF, not a WinForm. now I want to drop the same shadow on a winform.

This is what I want..¬

Windows Form Shadow

Not this..¬

Windows Form Shadow

like image 659
Sam Oyl Avatar asked May 11 '13 04:05

Sam Oyl


People also ask

How does drop shadow work?

CSS Demo: drop-shadow() The box-shadow property creates a rectangular shadow behind an element's entire box, while the drop-shadow() filter function creates a shadow that conforms to the shape (alpha channel) of the image itself.


2 Answers

In WinForms, you can just override the form's protected CreateParams property and add the CS_DROPSHADOW flag to the class styles. For example:

public class ShadowedForm : Form {     protected override CreateParams CreateParams {         get {             const int CS_DROPSHADOW = 0x20000;             CreateParams cp = base.CreateParams;             cp.ClassStyle |= CS_DROPSHADOW;             return cp;         }     }      // ... other code ... } 

But, a couple of caveats…

  1. This flag works only on top-level windows. In Win32-speak, that means overlapped and popup windows. It has no effect on child windows (e.g. controls). I thought I remembered hearing somewhere that this limitation had been removed from Windows 8, but I can't find a link confirming this and I don't have Windows 8 installed for testing purposes.

  2. It is possible that the user has disabled this feature altogether. If so, you won't get drop shadows, no matter how you ask for them. That's by design. Your application should not try and override this request. You can determine whether drop shadows are enabled or disabled by P/Invoking the SystemParametersInfo function and passing the SPI_GETDROPSHADOW flag.

  3. The Aero theme also adds shadows to top-level windows. This effect is separate and distinct from CS_DROPSHADOW, and works only when Aero is enabled. There's no way to turn it off and on for individual windows. Moreover, since the Aero theme has been removed from Windows 8, it won't ever have these shadows.

like image 118
Cody Gray Avatar answered Oct 26 '22 23:10

Cody Gray


Here's my C# implementation. It's similar to Al. Petro's, but you'll notice that when you lose focus and regain focus, the shadow repaints itself.

I've also added code to allow mouse-dragging.

public partial class Form1: Form {     [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]     private static extern IntPtr CreateRoundRectRgn     (         int nLeftRect, // x-coordinate of upper-left corner         int nTopRect, // y-coordinate of upper-left corner         int nRightRect, // x-coordinate of lower-right corner         int nBottomRect, // y-coordinate of lower-right corner         int nWidthEllipse, // height of ellipse         int nHeightEllipse // width of ellipse      );              [DllImport("dwmapi.dll")]     public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);      [DllImport("dwmapi.dll")]     public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);      [DllImport("dwmapi.dll")]     public static extern int DwmIsCompositionEnabled(ref int pfEnabled);      private bool m_aeroEnabled;                     // variables for box shadow     private const int CS_DROPSHADOW = 0x00020000;     private const int WM_NCPAINT = 0x0085;     private const int WM_ACTIVATEAPP = 0x001C;      public struct MARGINS                           // struct for box shadow     {         public int leftWidth;         public int rightWidth;         public int topHeight;         public int bottomHeight;     }      private const int WM_NCHITTEST = 0x84;          // variables for dragging the form     private const int HTCLIENT = 0x1;     private const int HTCAPTION = 0x2;      protected override CreateParams CreateParams     {         get         {             m_aeroEnabled = CheckAeroEnabled();              CreateParams cp = base.CreateParams;             if (!m_aeroEnabled)                 cp.ClassStyle |= CS_DROPSHADOW;              return cp;         }     }      private bool CheckAeroEnabled()     {         if (Environment.OSVersion.Version.Major >= 6)         {             int enabled = 0;             DwmIsCompositionEnabled(ref enabled);             return (enabled == 1) ? true : false;         }         return false;     }      protected override void WndProc(ref Message m)     {         switch (m.Msg)         {             case WM_NCPAINT:                        // box shadow                 if (m_aeroEnabled)                 {                     var v = 2;                     DwmSetWindowAttribute(this.Handle, 2, ref v, 4);                     MARGINS margins = new MARGINS()                     {                         bottomHeight = 1,                         leftWidth = 1,                         rightWidth = 1,                         topHeight = 1                     };                     DwmExtendFrameIntoClientArea(this.Handle, ref margins);                  }                 break;             default:                 break;         }         base.WndProc(ref m);          if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)     // drag the form             m.Result = (IntPtr)HTCAPTION;      }      public Form1()     {         m_aeroEnabled = false;          this.FormBorderStyle = FormBorderStyle.None;          InitializeComponent();     } } 
like image 32
Ryan Loggerythm Avatar answered Oct 27 '22 00:10

Ryan Loggerythm