在运行时通过鼠标拖动移动控件位置及改变控件的大小
来源:互联网 发布:大数据风控系统架构 编辑:程序博客网 时间:2024/05/17 05:19
public class Resize
{
bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
int Htap;
int Wtap;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public Resize(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;
this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X+this.Wtap)
locationX=this.frm.DesktopLocation.X+this.Wtap;
if(locationY<this.frm.DesktopLocation.Y+this.Htap)
locationY=this.frm.DesktopLocation.Y+this.Htap;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;
this.IsMoving=false;
this.ctrl.Refresh();
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
this.ctrl.Refresh();
}
}
}
{
bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
int Htap;
int Wtap;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public Resize(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;
this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X+this.Wtap)
locationX=this.frm.DesktopLocation.X+this.Wtap;
if(locationY<this.frm.DesktopLocation.Y+this.Htap)
locationY=this.frm.DesktopLocation.Y+this.Htap;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;
this.IsMoving=false;
this.ctrl.Refresh();
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
this.ctrl.Refresh();
}
}
}
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 在运行时通过鼠标拖动移动控件位置(c#)
- 在运行时通过鼠标拖动移动控件位置(c#)
- 在运行时通过鼠标拖动移动控件位置(c#)
- .net,C#如何在运行时通过鼠标拖动改变控件的大小
- C#如何在运行时通过鼠标拖动改变控件的大小
- Winform鼠标拖动改变控件大小、移动控件位置
- 在窗体运行后,可通过鼠标拖动控件来移动控件位置(c#) (转载)
- 在运行时使用鼠标移动控件和改变控件的大小
- 在运行时使用鼠标移动控件和改变控件的大小
- 在运行时使用鼠标移动控件和改变控件的大小
- 运行时拖动控件及调整控件大小的方法
- 运行时改变控件大小和位置
- 控件的鼠标拖动和改变大小实现的思考
- 鼠标拖动Panel控件,鼠标拖动改变位置
- 改控件的大小及移动控件的位置
- JSON 学习
- 使用post与使用get区别
- xml不带命名空间的序列化
- FTP再学习 (四)
- UML的术语-泛化,组合,依赖
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 面向对象的三个基本特征:封装、继承、多态
- test
- raid(转自网络)
- ASP.NET常用代码-javascript
- grub 学习笔记
- 优化的意外惊喜 (一)
- linux下的一些简单命令 9-5
- 牛市成就基金进入高价时代