بفرما این همون کلاسه:
کد:
public class ResizeableControl
{
private Control mControl;
private bool mMouseDown = false;
private EdgeEnum mEdge = EdgeEnum.None;
private int mWidth = 4;
private bool mOutlineDrawn = false;
private enum EdgeEnum
{
None,
Right,
Left,
Top,
Bottom,
TopLeft
}
public ResizeableControl(Control Control)
{
mControl = Control;
mControl.MouseDown += new MouseEventHandler(this.mControl_MouseDown);
mControl.MouseUp += new MouseEventHandler(this.mControl_MouseUp);
mControl.MouseMove += new MouseEventHandler(this.mControl_MouseMove);
mControl.MouseLeave += new System.EventHandler(this.mControl_MouseLeave);
}
private void mControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mMouseDown = true;
}
}
private void mControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mMouseDown = false;
}
private void mControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Control c = (Control)sender;
Graphics g = c.CreateGraphics();
switch (mEdge)
{
case EdgeEnum.TopLeft:
g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth * 4, mWidth * 4);
mOutlineDrawn = true;
break;
case EdgeEnum.Left:
g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth, c.Height);
mOutlineDrawn = true;
break;
case EdgeEnum.Right:
g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, 0, c.Width, c.Height);
mOutlineDrawn = true;
break;
case EdgeEnum.Top:
g.FillRectangle(Brushes.Fuchsia, 0, 0, c.Width, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.Bottom:
g.FillRectangle(Brushes.Fuchsia, 0, c.Height - mWidth, c.Width, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.None:
if (mOutlineDrawn)
{
c.Refresh();
mOutlineDrawn = false;
}
break;
}
if (mMouseDown & mEdge != EdgeEnum.None)
{
c.SuspendLayout();
switch (mEdge)
{
case EdgeEnum.TopLeft:
c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height);
break;
case EdgeEnum.Left:
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
break;
case EdgeEnum.Right:
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
break;
case EdgeEnum.Top:
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
break;
case EdgeEnum.Bottom:
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
break;
}
c.ResumeLayout();
}
else
{
if (e.X <= (mWidth * 4) & e.Y <= (mWidth * 4)){
//top left corner
c.Cursor = Cursors.SizeAll;
mEdge = EdgeEnum.TopLeft;
}else if ( e.X <= mWidth){
//left edge
c.Cursor = Cursors.VSplit;
mEdge = EdgeEnum.Left;
}else if ( e.X > c.Width - (mWidth + 1)){
//right edge
c.Cursor = Cursors.VSplit;
mEdge = EdgeEnum.Right;
}
else if (e.Y <= mWidth)
{
//top edge
c.Cursor = Cursors.HSplit;
mEdge = EdgeEnum.Top;
}else if ( e.Y > c.Height - (mWidth + 1)){
//bottom edge
c.Cursor = Cursors.HSplit;
mEdge = EdgeEnum.Bottom;
}else{
//no edge
c.Cursor = Cursors.Default;
mEdge = EdgeEnum.None;
}
}
}
private void mControl_MouseLeave(object sender, System.EventArgs e)
{
Control c = (Control)sender;
mEdge = EdgeEnum.None;
c.Refresh();
}
}
فقط کافیه مثلا توی لود فرم یا سازندهش مینویسید :
کد:
ResizeableControl rc = new ResizeableControl(button1);