Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Windows. forms;
Using system. drawing;
Namespace windowsapplication2
{
Class resizeaction
{
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 resizeaction (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 );
}
Public 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. Partition toplocation. x + this. wtap + this. CTRL. Location. X );
Int locationy = (cursor. position. Y-This. cursort + this. frm. Partition 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. Specify toplocation. X-This. wtap-This. CTRL. Location. X );
Int sizeagey = (cursor. position. Y-This. frm. Specify toplocation. 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 );
}
}
Public 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 );
}
Public 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. Specify toplocation. X-This. wtap;
This. CTRL. Top = This. ctrltop-This. frm. Specify toplocation. 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 ();
}
}
}
Call:
Private void form1_load (Object sender, eventargs E)
{
// Windowsapplication2.resizeaction rs = new windowsapplication2.resizeaction (this. label1, this );
Windowsapplication2.resizeaction rs = new windowsapplication2.resizeaction (this. button1, this );
}
Reference: http://www.cnblogs.com/DS-CzY/archive/2007/06/30/801377.aspx
this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/yanleigis/archive/2007/10/11/1819447.aspx