Csharp窗口FormBorderStyle属性None后拖动窗口代码

FormBorderStyle属性

属性 意义
FormBorderStyle.None 0 无边框
FormBorderStyle.FixedSingle 1 固定的单行边框
FormBorderStyle.Fixed3D 2 固定的三维样式边框
FormBorderStyle.FixedDialog 3 固定的对话框样式的粗边框
FormBorderStyle.Sizable 4 可调整大小的边框
FormBorderStyle.FixedToolWindow 5 不可调整大小的工具窗口边框
FormBorderStyle.SizableToolWindow 6 可调整大小的工具窗口边框

拖动窗口代码

方法1

        //FormBorderStyle.None时,支持改变窗体大小
        private const int Guying_HTLEFT = 10;
        private const int Guying_HTRIGHT = 11;
        private const int Guying_HTTOP = 12;
        private const int Guying_HTTOPLEFT = 13;
        private const int Guying_HTTOPRIGHT = 14;
        private const int Guying_HTBOTTOM = 15;
        private const int Guying_HTBOTTOMLEFT = 0x10;
        private 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;
                case 0x0201://鼠标左键按下的消息
                    m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
                    m.LParam = IntPtr.Zero; //默认值
                    m.WParam = new IntPtr(2);//鼠标放在标题栏内
                    base.WndProc(ref m);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

方法2

        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;
        //首先必须了解Windows的消息传递机制,当有鼠标活动消息时, 
        //系统发送WM_NCHITTEST 消息给窗体作为判断消息发生地的根据。 nchittest
        //假如你点击的是标题栏,窗体收到的消息值就是 HTCAPTION , 
        //同样地,若接受到的消息是 HTCLIENT,说明用户点击的是客户区,也就是鼠标消息发生在客户区。 
 
        //重写窗体,使窗体可以不通过自带标题栏实现移动
        protected override void WndProc(ref Message m)
        {
            //当重载窗体的 WndProc 方法时,可以截获 WM_NCHITTEST 消息并改些该消息, 
            //当判断鼠标事件发生在客户区时,改写改消息,发送 HTCAPTION 给窗体, 
            //这样,窗体收到的消息就时 HTCAPTION ,在客户区通过鼠标来拖动窗体就如同通过标题栏来拖动一样。 
            //注意:当你重载 WndProc 并改写鼠标事件后,整个窗体的鼠标事件也就随之改变了。 
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    base.WndProc(ref m);
                    if ((int)m.Result == HTCLIENT)
                        m.Result = (IntPtr)HTCAPTION;
                    return;
            }
            base.WndProc(ref m);
        }

panel控件移动

如果你在窗口中用panel布局了,那么就得使用以下代码来实现拖动。
panel_Title是panel的Name,添加事件MouseMove

       //panel控件移动
        private System.Drawing.Point mPoint; 
        private void panel_Title_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Location = new System.Drawing.Point(Location.X + e.X - mPoint.X, Location.Y + e.Y - mPoint.Y);
            }
        }
 
        private void panel_Title_MouseDown_1(object sender, MouseEventArgs e)
        {
            mPoint = new System.Drawing.Point(e.X, e.Y);
        }

发布日期:

所属分类: 编程, 编程语言 标签:


没有相关文章!