Winform自定義無邊框窗體


你還在為Winform原生窗體的丑陋而煩惱么?下面來看一下如何制作一個既漂亮又簡單的窗體

先看一下效果圖:

首先我們新建一個窗體FormM繼承原生Form

看一下主要的代碼

public partial class FormM : Form
    {
        public FormM()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 是否允許最大化
        /// </summary>
        private bool maxVisible = true;
        [Description("是否允許最大化")]
        public bool MaxVisible
        {
            get { return maxVisible; }
            set
            {
                maxVisible = value;
                if (!maxVisible)
                {
                    this.titleMin.Location = new System.Drawing.Point(titleMax.Location.X, 6);
                    titleMax.Visible = false;
                }
            }
        }
        /// <summary>
        /// 窗體標題
        /// </summary>
        private string titleText;
        [Description("窗體標題")]
        public string TitleText
        {
            get { return titleText; }
            set { titleText = value; }
        }
        /// <summary>
        /// 窗體標題是否顯示
        /// </summary>
        private bool titleVisible = true;
        [Description("窗體標題是否顯示")]
        public bool TitleVisible
        {
            get { return titleVisible; }
            set
            {
                titleVisible = value;
                title.Visible = titleVisible;
            }
        }
        /// <summary>
        /// 窗口默認大小
        /// FormSize.NORMAL OR FormSize.MAX
        /// </summary>
        private FormSize defaultFormSize = FormSize.NORMAL;
        [Description("窗口默認大小")]
        public FormSize DefaultFormSize
        {
            get { return defaultFormSize; }
            set
            {
                defaultFormSize = value;
                if (defaultFormSize == FormSize.MAX)
                {
                    //防止遮擋任務欄
                    this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                    this.WindowState = FormWindowState.Maximized;
                    //重置最大化圖標
                    this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max3;
                    this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max3;
                    this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max4;
                }
            }
        }

        /// <summary>
        /// 最小化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleMin_ButtonClick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// 最大化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleMax_ButtonClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                //重置最大化圖標
                this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max1;
                this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max1;
                this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max2;
            }
            else
            {
                //防止遮擋任務欄
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
                //重置最大化圖標
                this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max3;
                this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max3;
                this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max4;
            }
        }
        /// <summary>
        /// 關閉窗體
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleClose_ButtonClick(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 無邊框窗體移動、放大、縮小
        const int Guying_HTLEFT = 10;
        const int Guying_HTRIGHT = 11;
        const int Guying_HTTOP = 12;
        const int Guying_HTTOPLEFT = 13;
        const int Guying_HTTOPRIGHT = 14;
        const int Guying_HTBOTTOM = 15;
        const int Guying_HTBOTTOMLEFT = 0x10;
        const int Guying_HTBOTTOMRIGHT = 17;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0084:
                    base.WndProc(ref m);
                    Point vPoint = new Point((int)m.LParam & 0xFFFF,
                        (int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (vPoint.X <= 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)Guying_HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)Guying_HTBOTTOMLEFT;
                        else m.Result = (IntPtr)Guying_HTLEFT;
                    else if (vPoint.X >= ClientSize.Width - 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)Guying_HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)Guying_HTBOTTOMRIGHT;
                        else m.Result = (IntPtr)Guying_HTRIGHT;
                    else if (vPoint.Y <= 5)
                        m.Result = (IntPtr)Guying_HTTOP;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)Guying_HTBOTTOM;
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
        private Point mPoint;
        private void titleBar_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }
        private void titleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion

        public enum FormSize
        {
            NORMAL = 0,//正常大小
            MAX = 1,//最大化
        };
    }

解釋一下幾個關鍵的屬性

1 MaxVisible如果為false則表示無放大按鈕,默認為true,如下圖

2 TitleText 設置窗體左上角的標題如上圖中的Title

3 TitleVisible 設置窗體左上角的標題是否顯示,默認為顯示

4 DefaultFormSize 設置窗體首次打開時是否全屏,可取值為FormSize.NORMAL或FormSize.MAX,放大后的效果圖如下

最后介紹下如何使用

將FormM控件獨立打包成DLL庫,在其他項目中引用后,新建窗體時默認繼承的是Form,將其改為繼承FormM即可,如下

public partial class FormDemo : FormM
}

更多功能自己去完善,附上項目地址:

http://files.cnblogs.com/files/landptf/landptf.rar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM