winform移动窗体或窗体中的控件

新建类:winMove
using System;
using System.Collections.Generic;
using System.Text;

namespace WinExcelIEview
{
    using System.Windows.Forms;
    internal class winMove
    {
        private bool isMouseDown = false;
        private System.Drawing.Point FormLocation;     //form的location
        private System.Drawing.Point conObjLocation;     //Control的location
        private System.Drawing.Point mouseOffset;      //鼠标的按下位置
        private Form movedForm = null;
        private Control conObj = null;
        private int rectLeft = 0;
        private int rectRight = 0;

        private winMove()
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="movedForm">要移动的窗体</param>
        /// <param name="conObj">控制窗体移动的控件,为null,则是窗体本身</param>
        public winMove(Form movedForm, Control conObj)
        {
            this.movedForm = movedForm;
            this.conObj = (conObj==null)?movedForm:conObj;
            this.conObj.MouseDown += new MouseEventHandler(ConObj_MouseDown);
            this.conObj.MouseMove += new MouseEventHandler(ConObj_MouseMove);
            this.conObj.MouseUp += new MouseEventHandler(ConObj_MouseUp);
        }

        public winMove(Control conObj,int _rectLeft,int _right)
        {
            this.rectLeft = _rectLeft;
            this.rectRight = _right;
            this.conObj = conObj;
            this.conObj.MouseDown += new MouseEventHandler(ConObj_MouseDown2);
            this.conObj.MouseMove += new MouseEventHandler(ConObj_MouseMove2);
            this.conObj.MouseUp += new MouseEventHandler(ConObj_MouseUp);
        }
        private void ConObj_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = movedForm.Location;
                mouseOffset = Control.MousePosition;
                this.conObj.Cursor = System.Windows.Forms.Cursors.SizeAll;
            }
        }
        private void ConObj_MouseDown2(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                conObjLocation = conObj.Location;
                mouseOffset = Control.MousePosition;
                this.conObj.Cursor = System.Windows.Forms.Cursors.SizeAll;
            }
        }
        private void ConObj_MouseUp(object sender, MouseEventArgs e)
        {
            this.conObj.Cursor = System.Windows.Forms.Cursors.Default;
            isMouseDown = false;
        }

        private void ConObj_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int _x = 0;
                int _y = 0;
                System.Drawing.Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;
                movedForm.Location = new System.Drawing.Point(FormLocation.X - _x, FormLocation.Y - _y);
                
            }
        }
        private void ConObj_MouseMove2(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int _x = 0;
                int _y = 0;
                System.Drawing.Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;
                if (conObjLocation.X - _x < 0 || conObjLocation.Y - _y < 0
                    || conObjLocation.X - _x > rectLeft || conObjLocation.Y - _y > rectRight)
                {
                    return;
                }
                conObj.Location = new System.Drawing.Point(conObjLocation.X - _x, conObjLocation.Y - _y);

            }
        }
    }
}

在form窗体调用:

new winMove(this.Form1, this.panel2); //在panel2区域拖动form窗口//拖动paneShowAbout层,并限制拖动边界new winMove(this.paneShowAbout, (this.Width - this.paneShowAbout.Width), (this.Height - this.paneShowAbout.Height));



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值