winform窗体实现无边框带阴影效果的方法

本方法主要是通过绘制一个阴影窗体实现的,放大缩小都没问题,下面上代码:

1.首先定义一个通用处理类:

/// <summary>
/// 工具类
/// </summary>
public class FormStyleAPI
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Size
    {
        public Int32 cx;
        public Int32 cy;

        public Size(Int32 x, Int32 y)
        {
            cx = x;
            cy = y;
        }
    }

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

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

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

    public const byte AC_SRC_OVER = 0;
    public const Int32 ULW_ALPHA = 2;
    public const byte AC_SRC_ALPHA = 1;

    /// <summary>
    /// 从左到右显示
    /// </summary>
    public const Int32 AW_HOR_POSITIVE = 0x00000001;
    /// <summary>
    /// 从右到左显示
    /// </summary>
    public const Int32 AW_HOR_NEGATIVE = 0x00000002;
    /// <summary>
    /// 从上到下显示
    /// </summary>
    public const Int32 AW_VER_POSITIVE = 0x00000004;
    /// <summary>
    /// 从下到上显示
    /// </summary>
    public const Int32 AW_VER_NEGATIVE = 0x00000008;
    /// <summary>
    /// 若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
    /// </summary>
    public const Int32 AW_CENTER = 0x00000010;
    /// <summary>
    /// 隐藏窗口,缺省则显示窗口
    /// </summary>
    public const Int32 AW_HIDE = 0x00010000;
    /// <summary>
    /// 激活窗口。在使用了AW_HIDE标志后不能使用这个标志
    /// </summary>
    public const Int32 AW_ACTIVATE = 0x00020000;
    /// <summary>
    /// 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
    /// </summary>
    public const Int32 AW_SLIDE = 0x00040000;
    /// <summary>
    /// 透明度从高到低
    /// </summary>
    public const Int32 AW_BLEND = 0x00080000;
    #region MyRegion
    public const int WM_ERASEBKGND = 0x0014;
    public const int WM_LBUTTONDOWN = 0x0201;
    public const int WM_LBUTTONUP = 0x0202;
    public const int WM_LBUTTONDBLCLK = 0x0203;
    public const int WM_WINDOWPOSCHANGING = 0x46;
    public const int WM_PAINT = 0xF;
    public const int WM_CREATE = 0x0001;
    public const int WM_ACTIVATE = 0x0006;
    public const int WM_NCCREATE = 0x0081;
    public const int WM_NCCALCSIZE = 0x0083;
    public const int WM_NCPAINT = 0x0085;
    public const int WM_NCACTIVATE = 0x0086;
    public const int WM_NCLBUTTONDOWN = 0x00A1;
    public const int WM_NCLBUTTONUP = 0x00A2;
    public const int WM_NCLBUTTONDBLCLK = 0x00A3;
    public const int WM_NCMOUSEMOVE = 0x00A0;

    public const int WM_NCHITTEST = 0x0084;

    public const int HTLEFT = 10;
    public const int HTRIGHT = 11;
    public const int HTTOP = 12;
    public const int HTTOPLEFT = 13;
    public const int HTTOPRIGHT = 14;
    public const int HTBOTTOM = 15;
    public const int HTBOTTOMLEFT = 0x10;
    public const int HTBOTTOMRIGHT = 17;
    public const int HTCAPTION = 2;
    public const int HTCLIENT = 1;

    public const int WM_FALSE = 0;
    public const int WM_TRUE = 1;
    #endregion
    /// <summary>
    /// 执行动画
    /// </summary>
    /// <param name="whnd">控件句柄</param>
    /// <param name="dwtime">动画时间</param>
    /// <param name="dwflag">动画组合名称</param>
    /// <returns>bool值,动画是否成功</returns>
    [DllImport("user32")]
    public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);

    [DllImport("user32")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    [DllImport("gdi32.dll")]
    public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);

    [DllImport("user32.dll")]
    public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);

    [DllImport("user32", EntryPoint = "GetWindowLong")]
    public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32", EntryPoint = "SetWindowLong")]
    public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

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

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

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

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

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

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

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

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
}

2.定义一个皮肤窗体ShadowFormSkin:(设计窗代码就不提供了,直接新建一个窗体就行)

partial class ShadowFormSkin : Form
{
    private Point _MouseLocation;
    internal Point MouseLocation { get { return _MouseLocation; } }
    private ShadowForm Main;
    public ShadowFormSkin(ShadowForm main)
    {
        InitializeComponent();
        SetStyles();//减少闪烁
        Main = main;//获取控件层对象
        Text = main.Text;//设置标题栏文本
        Icon = main.Icon;//设置图标
        TopMost = main.TopMost;
        FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
        ShowInTaskbar = true;//显示阴影层到任务栏(win10可显示缩略图)
        Size = Main.Size;//统一大小
        Width += (Main.ShadowWidth * 2);
        Height += (Main.ShadowWidth * 2);
        Main.Owner = this;//设置控件层的拥有皮肤层
        //SetBits();//绘制半透明不规则皮肤
    }
    private void ShadowFormSkin_Load(object sender, EventArgs e)
    {
        DrawShadow();
    }
    #region 减少闪烁
    private void SetStyles()
    {
        SetStyle(
            ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.DoubleBuffer, true);
        //强制分配样式重新应用到控件上
        UpdateStyles();
        base.AutoScaleMode = AutoScaleMode.None;
    }
    #endregion
    #region 绘图
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cParms = base.CreateParams;
            cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
            return cParms;
        }
    }
    internal void DrawShadow()
    {
        Bitmap bitmap = null;
        Graphics g = null;
        try
        {
            bitmap = new Bitmap(Width, Height);
            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 < Main.ShadowWidth; i++)
            {
                p.Color = Color.FromArgb((255 / 10 / Main.ShadowWidth) * i, c);
                //g.DrawRectangle(p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1));
                DrawRoundRectangle(g, p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1), Main.ShadowWidth - i);
            }
            SetBits(bitmap);
        }
        catch { }
        finally
        {
            g?.Dispose();
            bitmap?.Dispose();
        }
    }
    public void SetBits(Bitmap bitmap)
    {
        if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
            throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
        IntPtr oldBits = IntPtr.Zero;
        IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
        IntPtr hBitmap = IntPtr.Zero;
        IntPtr memDc = FormStyleAPI.CreateCompatibleDC(screenDC);

        try
        {
            FormStyleAPI.Point topLoc = new FormStyleAPI.Point(Left, Top);
            FormStyleAPI.Size bitMapSize = new FormStyleAPI.Size(Width, Height);
            FormStyleAPI.BLENDFUNCTION blendFunc = new FormStyleAPI.BLENDFUNCTION();
            FormStyleAPI.Point srcLoc = new 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);
        }
        finally
        {
            if (hBitmap != IntPtr.Zero)
            {
                FormStyleAPI.SelectObject(memDc, oldBits);
                FormStyleAPI.DeleteObject(hBitmap);
            }
            FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDC);
            FormStyleAPI.DeleteDC(memDc);
        }
    }
    #endregion
    #region MyRegion
    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;
    }
    #endregion
}

3.实现阴影效果窗体,该窗体需要作为其他窗体的基础类直接继承就可以了:

 public partial class ShadowForm : Form
 {
     private Point _MouseLocation;
     internal Point MouseLocation { get { return _MouseLocation; } }
     internal int ShadowWidth = 15;
     private ShadowFormSkin Skin;
     public ShadowForm()
     {
         InitializeComponent();
         //SetStyles();//减少闪烁
         FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
         ShowInTaskbar = false;
     }
     private void IrregularForm_Load(object sender, EventArgs e)
     {
         if (!DesignMode)
         {
             Skin = new ShadowFormSkin(this);//创建皮肤层
             ShadowForm_LocationChanged(null, null);
             Skin.BackColor = Color.Red;
             Skin.Show();//显示皮肤层
         }
     }
     #region 减少闪烁
     private void SetStyles()
     {
         SetStyle(
             ControlStyles.UserPaint |
             ControlStyles.AllPaintingInWmPaint |
             ControlStyles.OptimizedDoubleBuffer |
             ControlStyles.ResizeRedraw |
             ControlStyles.DoubleBuffer, true);
         //强制分配样式重新应用到控件上
         UpdateStyles();
         base.AutoScaleMode = AutoScaleMode.None;
     }
     #endregion
     /// <summary>
     /// 窗体显示状态
     /// </summary>
     /// <param name="value"></param>
     public void Visibility(bool value)
     {
         if (value)
         {
             Show();
             Skin.Show();
         }
         else
         {
             Hide();
             Skin.Hide();
         }
     }

     public void MinForm()
     {
         this.WindowState = FormWindowState.Minimized;
         this.Skin.WindowState = FormWindowState.Minimized;
     }


     protected override void OnMouseMove(MouseEventArgs e)
     {
         base.OnMouseMove(e);
         _MouseLocation = e.Location;
         if (e.Button == MouseButtons.Left)
         {
             FormStyleAPI.ReleaseCapture();
             FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
         }
     }
     private void ShadowForm_LocationChanged(object sender, EventArgs e)
     {
         if (Skin != null)
         {
             Skin.Location = new Point(Left - ShadowWidth, Top - ShadowWidth);
             Skin.DrawShadow();
         }
     }
 }

4.使用方法,直接在需要实现效果的窗体去继承就可以了,例如:

 public partial class frmMain : ShadowForm

结语:使用过程中有一点需要注意,在窗体最小化的时候,需要调用一下Visibility(true),不然会在桌面有一个阴影的窗体效果在那里。显示的时候再调用一下。

public void SetBits() { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.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.main_light_bkg_top123, 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); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值