winform 可拖动无边框窗体解决办法

 

方法一:通过重载消息处理实现。

鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动

winform 可拖动无边框窗体解决办法
/// <summary>
        /// 通过重载消息处理实现。重写窗口过程(WndProc),处理一些非客户区消息(WM_NCxxxx),
        /// C#中重写窗口过程不用再调用SetWindowLong API了,直接overide一个WndProc就可以了,不用声明api函数
        /// 鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x84)
            {
                switch (m.Result.ToInt32())
                {
                    case 1:
                        m.Result = new IntPtr(2);
                        break;
                }
            }
        }
View Code

 

方法二:调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体

winform 可拖动无边框窗体解决办法
/// <summary>
        /// 调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("User32.DLL")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("User32.DLL")]
        public static extern bool ReleaseCapture();
        public const uint WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 61456;
        public const int HTCAPTION = 2;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
        }
View Code

 

方法三:直接在控件上写事件,朋友的是一个PictureBox 停靠在主窗体,然后主窗体设置的无边框,用的是这中方法

winform 可拖动无边框窗体解决办法
/// <summary>
        /// 直接在控件上写事件,朋友的是一个PictureBox 停靠在主窗体,然后主窗体设置的无边框,用的是这中方法
        /// </summary>
        Point downPoint;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = new Point(e.X, e.Y);
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }
View Code

 

.net 重写URL:http://www.cnblogs.com/yonsy/archive/2012/09/21/2696935.html

 

winform 可拖动无边框窗体解决办法,布布扣,bubuko.com

winform 可拖动无边框窗体解决办法

上一篇:C#winform项目添加引用编译文件


下一篇:录制脚本启动报错:target controller is configured to “use recording Controller” but no such controller exists,ensure you add a Recording Controller as child of Thread Group node to start recording correctly