动态改变大小
2008-07-25 15:52
183 查看
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();
}
}
}
该文章转载自网络大本营:file:///D:/OCR/ziliao/在运行时通过鼠标拖动移动控件位置及改变控件的大小__Net技术库_网络大本营.htm
{
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();
}
}
}
该文章转载自网络大本营:file:///D:/OCR/ziliao/在运行时通过鼠标拖动移动控件位置及改变控件的大小__Net技术库_网络大本营.htm
相关文章推荐
- unity基础开发-- 动态改变模型大小、方向
- Android 动态改变组件大小
- 求助!如何在动态改变大小的Image上画图!
- Android TextView 动态改变字体颜色、大小
- Ext3.2.2 表格随窗口改变动态适应窗体大小
- 用js实现层随着内容大小动态渐变改变 推荐
- TWebBrower 动态改变大小
- jQuery动态改变图片显示大小(修改版)
- easyui datagrid 动态改变大小
- 在iframe的name不定时,根据被包含页面大小动态改变iframe的大小(by quqi99)
- 动态改变dialog对话框大小
- 动态改变空间的位置和大小
- ugui学习——可动态改变Content大小的ScrollView
- MFC改变对话框的大小,以及子控件的动态改变
- 一个动态改变模型大小、方向的例子
- android 动态改变屏幕亮度和声音大小方式
- android 动态改变布局控件 的大小
- javascript动态改变div大小
- 数据动态改变大小?向高手求教
- jsp页面跳转到子页面和返回上一页,附带动态改变父页面的iframe高度大小