winForm實現拖動無邊框窗體

在有時候我們爲了實現軟件的美觀,我們需要把窗體的邊框隱藏,但是問題也會隨之而來,在屬性中設置了FormBorderStyle應該爲None,邊框便可以隱藏,但是我們在使用軟件時卻無法拖動窗體,那麼我們該如何解決這個問題呢。其實代碼很簡單,只要override   WndProc方法便可。在無邊框窗體的代碼中加入下面的protected override void WndProc(ref Message m)方法便可。具體代碼如下:
using System;
using System.Windows.Forms;

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

        #region 移動窗體
        /// <summary>
        /// 重寫WndProc方法,實現窗體移動和禁止雙擊最大化
        /// </summary>
        /// <param name="m">Windows 消息</param>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x4e:
                case 0xd:
                case 0xe:
                case 0x14:
                    base.WndProc(ref m);
                    break;
                case 0x84://鼠標點任意位置後可以拖動窗體
                    this.DefWndProc(ref m);
                    if (m.Result.ToInt32() == 0x01)
                    {
                        m.Result = new IntPtr(0x02);
                    }
                    break;
                case 0xA3://禁止雙擊最大化
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
        #endregion
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章