无边框窗体拖拽拉大,通过API

private const int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
        private const int HTCLIENT = 0x1;//工作区
        private const int HTSYSMENU = 3;//系统菜单
        private const int HTCAPTION = 0x2; //标题栏

        private const int HTLEFT = 10;//向左
        private const int HTRIGHT = 11;//向右
        private const int HTTOP = 12;//向上
        private const int HTTOPLEFT = 13;//向左上
        private const int HTTOPRIGHT = 14;//向右上
        private const int HTBOTTOM = 15;//向下
        private const int HTBOTTOMLEFT = 16;//向左下
        private const int HTBOTTOMRIGHT = 17;//向右下

        private const int BorderWidth = 5;//自己定义的窗体边的宽度

        //可以调整窗体的大小和移动窗体的位置
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    base.WndProc(ref m);
                    if (DesignMode)
                    {
                        return;
                    }

                    if ((int)m.Result == HTCLIENT)//在客户区
                        if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
                            m.Result = (IntPtr)HTTOPLEFT;//左上
                        else if ((Cursor.Position.X >= this.Left + this.Width - BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
                            m.Result = (IntPtr)HTTOPRIGHT;//右上
                        else if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y >= this.Top + this.Height - BorderWidth))
                            m.Result = (IntPtr)HTBOTTOMLEFT;//左下
                        else if ((Cursor.Position.X >= this.Left + this.Width - BorderWidth) && (Cursor.Position.Y >= this.Top + this.Height - BorderWidth))
                            m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
                        else if (Cursor.Position.X <= this.Left + BorderWidth)
                            m.Result = (IntPtr)HTLEFT;//左
                        else if (Cursor.Position.X >= this.Left + this.Width - BorderWidth)
                            m.Result = (IntPtr)HTRIGHT;//右
                        else if (Cursor.Position.Y <= this.Top + BorderWidth)
                            m.Result = (IntPtr)HTTOP;//上
                        else if (Cursor.Position.Y >= this.Top + this.Height - BorderWidth)
                            m.Result = (IntPtr)HTBOTTOM;//下
                        else if (Cursor.Position.Y <= this.Top + BorderWidth + 10)
                        {
                            if (Cursor.Position.X <= this.Left + this.Width - BorderWidth + 10)
                            {
                                m.Result = (IntPtr)HTSYSMENU;//系统菜单,可以在这里双击鼠标关闭窗体。
                            }
                        }
                        else
                            m.Result = (IntPtr)HTCAPTION;//移动窗体,双击可以最大化窗体或还原。
                    return;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值