Cómo agregar sombra a todos los bordes en formato C#
El método que uso es dibujar la sombra en el mapa de bits y luego usar el principio de la forma de doble capa para dibujar el mapa de bits en la capa de fondo.
Los más importantes son:
1. Dibujar un rectángulo redondeado
public static void DrawRoundRectangle(Gráficos g, Pen pen, Rectángulo rect, int cornerRadius)
{
usando (ruta GraphicsPath = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.DrawPath(pluma, ruta) ;
}
}
public static void FillRoundRectangle(Gráficos g, Pincel, Rectángulo rect, int cornerRadius)
{
usando (ruta GraphicsPath = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.FillPath(pincel, ruta);
}
}
GraphicsPath estático interno CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
redondeadoRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
redondeadoRect.AddLine(rect.X + cornerRadius, rect. Y, rect.Right - cornerRadius * 2, rect.Y);
redondeadoRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
redondeadoRect.AddLine(rect.Derecha, rect.Y + radio de esquina * 2, rect.Derecha, rect.Y + altura rect - Radio de esquina * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
redondeadoRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
redondeadoRect.AddLine(rect
.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;} p>
2. Dibujar sombra
vacío interno DrawShadow()
{
Mapa de bits bitmap = null;
Gráficos g = null; probar
{
mapa de bits = nuevo mapa de bits (ancho, alto);
g = Graphics.FromImage(mapa de bits);
g.SmoothingMode = SmoothingMode.AntiAlias;
Color c = Color.FromArgb(0, 0, 0, 0);
Pluma p = nueva Pluma(c, 3) ; for (int i = 0; i < Main.ShadowWidth; i++)
{
p.Color = Color.FromArgb((255 / 10 / Main.ShadowWidth) * i , c);
DrawRoundRectangle(g, p, new Rectángulo(i, i, Ancho - (2 * i) - 1, Alto - (2 * i) - 1), Main.ShadowWidth - i );
}
SetBits(mapa de bits);
} captura { } finalmente
{
g ?.Dispose();
mapa de bits?.Dispose();
}
}
3. mapa de bits en ventana
anulación protegida CreateParams CreateParams
{
get
{
CreateParams cParms = base .CreateParams ;
cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
devuelve cParms;
}
}
public void SetBits(Bitmap bitmap)
{
if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat)) p>
throw new ApplicationException("La imagen debe ser de 32 bits con canal Alhpa.
");
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = FormStyleAPI.CreateCompatibleDC(screenDC);
prueba
{
FormStyleAPI.Point topLoc = new FormStyleAPI.Point( Izquierda, Arriba);
FormStyleAPI.Size bitMapSize = new FormStyleAPI.Size(Ancho, Alto);
FormStyleAPI.BLENDFUNCTION blendFunc = new FormStyleAPI.BLENDFUNCTION();
FormStyleAPI.Point srcLoc = nuevo FormStyleAPI.Point(0, 0);
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBits = FormStyleAPI. SelectObject(memDc, hBitmap);
blendFunc.BlendOp = FormStyleAPI.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = Byte.Parse(((int)(Main.Opacity * 255)). ToString());
blendFunc.AlphaFormat = FormStyleAPI.AC_SRC_ALPHA;
blendFunc.BlendFlags = 0;
FormStyleAPI.UpdateLayeredWindow(Handle, screenDC, ref topLoc , ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, FormStyleAPI.ULW_ALPHA);
}
finalmente
{
if (hBitmap != IntPtr.Zero)
{
FormStyleAPI.SelectObject(memDc, oldBits);
FormStyleAPI.DeleteObject(hBitmap);
}
FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDC);
FormStyleAPI.DeleteDC(memDc);
}
}
4. El efecto y el código fuente de la solución final