CSharp Windows Form 使用圓邊EdgeRounding

Windows Form的屬性設定裡並無法直接設定成圓邊,如果需要在FormBoderStyle為none的狀態下讓Form以圓邊的狀態下顯示,如下圖:

01.jpg




只需要把Form的可視範圍畫出成圓邊的路徑就可以了,其它元件也可以那麼做,只是當元件被重畫時,需要重新Repaint圓邊的範圍。

            GraphicsPath graphicpath = new GraphicsPath();
            graphicpath.StartFigure();
            graphicpath.AddArc(0, 0, 25, 25, 180, 90);
            graphicpath.AddLine(25, 0, this.Width - 25, 0);
            graphicpath.AddArc(this.Width - 25, 0, 25, 25, 270, 90);
            graphicpath.AddLine(this.Width, 25, this.Width, this.Height - 25); 
            graphicpath.AddArc(this.Width - 25, this.Height - 25, 25, 25, 0, 90);
            graphicpath.AddLine(this.Width - 25, this.Height, 25, this.Height);
            graphicpath.AddArc(0, this.Height - 25, 25, 25, 90, 90); 
            graphicpath.CloseFigure();
            this.Region = new Region(graphicpath);

如果需要內圓邊則可利用背景圖畫出內圓邊範圍,再設定BackgroundImageLayout為Stretch,則會自動縮成跟Form一樣大小。

如果縮放Form可以看到圓邊也跟著重畫,而內圓也會自動縮成合適的大小,如下圖:

02.jpg

原始碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace EdgeRounding
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RePaint();
        }

        protected void RePaint()
        {

            GraphicsPath graphicpath = new GraphicsPath();

            graphicpath.StartFigure();

            graphicpath.AddArc(0, 0, 25, 25, 180, 90);

            graphicpath.AddLine(25, 0, this.Width - 25, 0);

            graphicpath.AddArc(this.Width - 25, 0, 25, 25, 270, 90);

            graphicpath.AddLine(this.Width, 25, this.Width, this.Height - 25);

            graphicpath.AddArc(this.Width - 25, this.Height - 25, 25, 25, 0, 90);

            graphicpath.AddLine(this.Width - 25, this.Height, 25, this.Height);

            graphicpath.AddArc(0, this.Height - 25, 25, 25, 90, 90);

            graphicpath.CloseFigure();

            this.Region = new Region(graphicpath);


        }
        private Point startPoint;
        // Saves the resize origin
        private void resizeBox_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint = new Point(-e.X + this.pictureBox1.Width, -e.Y + this.pictureBox1.Height);
        }

        // Resizes the form 
        private void resizeBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(startPoint.X, startPoint.Y);
                Size = new Size(mousePos.X - Location.X, mousePos.Y - Location.Y);
                //重畫圓邊
                RePaint();

            }
        }


        //移動畫面
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint = new Point(-MousePosition.X + Location.X, -MousePosition.Y + Location.Y);
        }
        //移動畫面
        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(startPoint.X, startPoint.Y);
                Location = mousePos;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}

下載點:

WebHd

One thought to “CSharp Windows Form 使用圓邊EdgeRounding”

  1. Dear 筆者,
    我叫Andre,我是艾鍗學院的教務人員。

    近來我一直在找尋Android講師,希望可以找到合適的講師人選,

    在看過您所撰寫的文章後,希望能有機會與您認識。

    Andre

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料