您的位置:首页 > 其它

实现窗体皮肤美化后进行窗体的移动

2010-05-24 21:22 239 查看
窗体美化(将窗体属性FormBorderStyle设置为none)发现整个窗体不能随鼠标移动了,我曾经用以下代码解决过窗体的移动:

  #region 实现点击移动

        internal static int WM_NCHITTEST = 0x84;
        internal static IntPtr HTCLIENT = (IntPtr)0x1;
        internal static IntPtr HTCAPTION = (IntPtr)0x2;
        internal static int WM_NCLBUTTONDBLCLK = 0x00A3;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDBLCLK)
            {
                return;
            }
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (m.Result == HTCLIENT)
                {
                    m.HWnd = this.Handle;
                    m.Result = HTCAPTION;
                }
                return;
            }
            base.WndProc(ref m);
        }

        #endregion

 

 

不过发现,按住标题栏(这个标题栏我是用的pannel制作的)没反应,郁闷好几天,最终找到了解决的方法,就是用:

MouseDown和MouseMove事件解决。

具体方法如下:

 

在窗体的顶端声明:

Point MyOffset;//记录窗体位置

 

回到设计模式,选中顶端标题栏,选择MouseDown事件:

代码:

MyOffset = new Point(-e.X, -e.Y);

MouseMove事件代码:

 //重置窗体位置
            if (e.Button == MouseButtons.Left)
            {
                Point MyPos = Control.MousePosition;
                MyPos.Offset(MyOffset.X, MyOffset.Y);
                //this.Location =MyPos;
                this.DesktopLocation = MyPos;
            }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式