winform移动窗体或窗体中的控件
2013-05-10 13:37
253 查看
新建类: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));
相关文章推荐
- winform窗体中移动控件
- Winform窗体可自由移动用户控件
- C#winform窗体中如何点击移动某个控件来移动窗体
- winform 移动窗体控件图解
- Winform中窗体中两用户控件传值
- WebBrowser 控件 和winform 窗体的双向操作(转)
- WinForm--(子窗体改变父窗体中控件的属性-委托)
- winform添加圆角窗体+移动窗体
- C# WinForm窗体及其控件的自适应
- WinForm窗体没有系统边框的时候移动窗体
- c#,VC++ 在WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- winform窗体 控件【对话框】
- C# Winform主窗体和子窗体之间如何相互操作对方的控件
- C# Winform 左侧菜单右边显示、子窗体显示在主窗体(子窗体当控件添加到Panel)
- 在.Net的WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- wap移动用户控件和wap窗体引用格式
- WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件)
- 在窗体上移动控件
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- WinForm 子窗体在父窗体范围内移动,不能出父窗体