天天看點

Winform無邊框圓角化移動窗體

/// <summary>
    /// 窗體圓角化,滑鼠移動窗體
    /// </summary>
    public static class FrmYJH
    {
        #region 滑鼠移動窗體
        static int x, y; //滑鼠點選坐标位置

        public static void FrmMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
            }
        }
        public static void FrmMouseMove(Form frm,object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                frm.Location = new Point(frm.Location.X + (e.X - x), frm.Location.Y + (e.Y - y));
            }
        }
        #endregion

        #region 初始化視窗圓角化
        public static void FrmResize(Form frm,object sender, EventArgs e)
        {
            if (frm.WindowState == FormWindowState.Normal)
            {
                SetWindowRegion(frm);
            }
            else
            {
                frm.Region = null;
            }
        }

        public static void SetWindowRegion(Form frm)
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, frm.Width, frm.Height);
            FormPath = GetRoundedRectPath(rect, 30);
            frm.Region = new Region(FormPath);
        }
        public static GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            System.Drawing.Drawing2D.GraphicsPath path = new GraphicsPath();

            path.AddArc(arcRect, 180, 90);//左上角

            arcRect.X = rect.Right - diameter;//右上角
            path.AddArc(arcRect, 270, 90);

            arcRect.Y = rect.Bottom - diameter;// 右下角
            path.AddArc(arcRect, 0, 90);

            arcRect.X = rect.Left;// 左下角
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }

        #endregion
    }