C#窗体阴影效果,不好用。先用一张图片,然后将图片转得到底色,不好用

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
public partial class SkinForm : Form
{
int BordWidth;
int BordHeight;
Point loc;
public SkinForm(Form father)
{
InitializeComponent();
BordWidth = father.Size.Width;
BordHeight = father.Size.Height;
loc = father.Location;
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern long GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
public static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags);
const int GWL_EXSTYLE = -20;
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_LAYERED = 0x80000;
const int LWA_ALPHA = 2;

private void Form1_Load(object sender, EventArgs e)
{
//SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
//var bitmap = Properties.Resources.pic_zj_ty;// ZoomImage(global::WinFormsApp1.Properties.Resources.pic_zj_ty, BordHeight, BordWidth);
//SetBits(bitmap);
DrawShadow();

}
public static void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.DrawPath(pen, path);
}
}
public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.FillPath(brush, path);
}
}
internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 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);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
void DrawShadow()
{
Bitmap bitmap = null;
Graphics g = null;
try
{
bitmap = new Bitmap(BordWidth, BordHeight);
g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;

Color c = Color.FromArgb(0, 0, 0, 0);
Pen p = new Pen(c, 3);

for (int i = 0; i < BordWidth; i++)
{
p.Color = Color.FromArgb((255 / 10 / BordWidth) * i, c);
DrawRoundRectangle(g, p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1), BordWidth - i);
}
SetBits(bitmap);
}
catch { }
finally
{
g?.Dispose();
bitmap?.Dispose();
}
}
int bdWidth = 20;
public void SetBits(Image bitmap)
{
            //if (!haveHandle) return;
            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
try
{
Win32.Point topLoc = new Win32.Point(loc.X - 20, loc.Y - bdWidth);
//Win32.Size bitMapSize = new Win32.Size(BordWidth + 190, BordHeight + 290);
Win32.Size bitMapSize = new Win32.Size(BordWidth + bdWidth * 2, BordHeight + bdWidth * 2);
Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
Win32.Point srcLoc = new Win32.Point(0, 0);
hBitmap = ((Bitmap)bitmap).GetHbitmap(Color.FromArgb(0));
oldBits = Win32.SelectObject(memDc, hBitmap);
blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = 255;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = 0;
Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
}
finally
{
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBits);
Win32.DeleteObject(hBitmap);
}
Win32.ReleaseDC(IntPtr.Zero, screenDC);
Win32.DeleteDC(memDc);
}
}
public static Image ZoomImage(Image bitmap, int destHeight, int destWidth)
{
try
{
Image sourImage = bitmap;
int width = 0, height = 0;
//按比例缩放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if (sourHeight > destHeight || sourWidth > destWidth)
{
if ((sourWidth * destHeight) > (sourHeight * destWidth))
{
width = destWidth;
height = (destWidth * sourHeight) / sourWidth;
}
else
{
height = destHeight;
width = (sourWidth * destHeight) / sourHeight;
}
}
else
{
width = sourWidth;
height = sourHeight;
}
Bitmap destBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent);
//设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}
}

class Win32
{
public enum Bool
{
False = 0,
True
};

[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public Int32 x;
public Int32 y;

public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
}

[StructLayout(LayoutKind.Sequential)]
public struct Size
{
public Int32 cx;
public Int32 cy;

public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ARGB
{
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}


public const Int32 ULW_COLORKEY = 0x00000001;
public const Int32 ULW_ALPHA = 0x00000002;
public const Int32 ULW_OPAQUE = 0x00000004;

public const byte AC_SRC_OVER = 0x00;
public const byte AC_SRC_ALPHA = 0x01;


[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteObject(IntPtr hObject);
}
}


//public void SetBits()
//{
// //绘制绘图层背景
// Bitmap bitmap = new Bitmap(Width + 10, Height + 10);
// Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界
// Graphics g = Graphics.FromImage(bitmap);
// g.SmoothingMode = SmoothingMode.HighQuality; //高质量
// g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
// ImageDrawRect.DrawRect(g, Properties.Resources.pic_zj_ty, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1);

// if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
// throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
// IntPtr oldBits = IntPtr.Zero;
// IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
// IntPtr hBitmap = IntPtr.Zero;
// IntPtr memDc = Win32.CreateCompatibleDC(screenDC);

// try
// {
// Win32.Point topLoc = new Win32.Point(Left, Top);
// Win32.Size bitMapSize = new Win32.Size(Width, Height);
// Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
// Win32.Point srcLoc = new Win32.Point(0, 0);

// hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
// oldBits = Win32.SelectObject(memDc, hBitmap);

// blendFunc.BlendOp = Win32.AC_SRC_OVER;
// blendFunc.SourceConstantAlpha = Byte.Parse("255");
// blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
// blendFunc.BlendFlags = 0;

// Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
// }
// finally
// {
// if (hBitmap != IntPtr.Zero)
// {
// Win32.SelectObject(memDc, oldBits);
// Win32.DeleteObject(hBitmap);
// }
// Win32.ReleaseDC(IntPtr.Zero, screenDC);
// Win32.DeleteDC(memDc);
// }

 

posted @ 2021-04-08 10:06  十年之前,十年之后  阅读(191)  评论(0)    收藏  举报