Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows 7 style Dropshadow in borderless form
    text
    copied!<blockquote> <p><strong><em>Short Version:</em></strong></p> </blockquote> <p><strong>Goal:</strong> A deep, dark, Windows 7 dropshadow in borderless WinForm in C#</p> <hr> <p><strong>Known existing solutions 1:</strong> Simple XP-style dropshadow using CreateParams.</p> <p><strong>Problem:</strong> Too weak, too light, too ugly.</p> <hr> <p><strong>Known existing solutions 2:</strong> Replace GDI of form with bitmap.</p> <p><strong>Problem:</strong> Lose the ability to use controls, only functional as a splash screen.</p> <hr> <p><strong>Objective by this post:</strong> Find a median solution to this problem or an all together better one. </p> <p>. . .</p> <blockquote> <p><strong><em>Long Version:</em></strong></p> </blockquote> <p>(Edit: I am referring to the drop-shadow going along the border of any windows form, if that wasn't clear.) I understand that there is a way to make XP style dropshadows in C# using: </p> <p><strong>C# Code 1 - Simple XP-style dropshadow (Problem: to light, to weak, to ugly)</strong></p> <pre><code>// 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; } } </code></pre> <p>However, I am trying to figure out how to make them appear like the do in Windows 7 (deeper and larger shadows) and can't figure out the best way of doing this. </p> <p>I have a method now created that will let me override the entire form GDI and appear like a splash screen would (credit not mine):</p> <p><strong>C# Code 2: Replace form GDI with Bitmap (Problem: can't use form controls, hard to maintain GUI)</strong> </p> <pre><code> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style return cp; } } </code></pre> <p>However, this does give me a full 32-bit background (as I require to add the dropshadow manually), but I lose the ability to create form elements that are visible. </p> <p>So basically, I am trying to figure out a median between these two methods. Something that will give me deep and dark drop shadows without losing other functionality / causing excessive repainting requirements. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload