Move the widget position and change the widget size when running
Last Update:2018-12-03
Source: Internet
Author: User
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. Specify toplocation. x + this. wtap + this. CTRL. Location. X );
Int locationy = (cursor. position. Y-this.cursorT + this. frm. Specify toplocation. Y + this. HTAP + this. CTRL. Location. y );
If (locationx <this. frm. Specify toplocation. x + this. wtap)
Locationx = This. frm. Specify toplocation. x + this. wtap;
If (locationy <this. frm. Specify toplocation. Y + this. HTAP)
Locationy = This. frm. Specify toplocation. 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. specified toplocation. X + this. CTRL. left + this. wtap, this. FRM. specified toplocation. 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. specified toplocation. X + this. wtap + this. CTRL. left, this. FRM. specified toplocation. 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. Specify toplocation. x + this. wtap + this. CTRL. Left;
This. ctrltop = This. frm. Specify toplocation. 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. specified toplocation. X + this. wtap + this. CTRL. left, this. FRM. specified toplocation. 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 ();
}
}
}