using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; namespace WindowsApplication4
{
public class TrackCom : System.Windows.Forms.Label
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private const int Rectsize = 6;
private Point ComStartPoint;
private bool ComDragFlag;
private Control BindControl;
private Rectangle[] rect;
private int SelDragDirect;
private Label FrontLabel; public TrackCom(Control cont)
{
// This call is required by the Windows.Forms Form Designer. this.Parent = cont.Parent;
this.Top = cont.Top - Rectsize ;
this.Left = cont.Left - Rectsize ;
this.Width = cont.Width + 2 * Rectsize ;
this.Height = cont.Height + 2 * Rectsize ;
this.BringToFront();
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.TrackCom_Paint);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown);
this.Move += new System.EventHandler(this.TrackCom_Move);
this.Resize += new System.EventHandler(this.TrackCom_Resize); FrontLabel = new Label();
FrontLabel.Parent = cont;
FrontLabel.BackColor = System.Drawing.Color.Transparent;
FrontLabel.Size = cont.Size;
FrontLabel.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
FrontLabel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
FrontLabel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
FrontLabel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
FrontLabel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown); BindControl = cont;
BindControl.Parent = this;
BindControl.Top = Rectsize ;
BindControl.Left = Rectsize ;
SetRect();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing)
{
BindControl.Parent = this.Parent;
BindControl.Top = this.Top + Rectsize ;
BindControl.Left = this.Left + Rectsize ;
FrontLabel.Dispose();
if( disposing )
{
if(components != null)
{ components.Dispose();
}
}
base.Dispose( disposing );
} private void SetRect()
{
rect = new Rectangle[8];
rect[0] = new Rectangle(0,0,5,5);
rect[1] = new Rectangle((Width - Rectsize)/2,0,5,5);
rect[2] = new Rectangle(Width - Rectsize,0,5,5);
rect[3] = new Rectangle(0,(Height - Rectsize)/2,5,5);
rect[4] = new Rectangle(Width - Rectsize,(Height - Rectsize)/2,5,5);
rect[5] = new Rectangle(0,Height - Rectsize,5,5);
rect[6] = new Rectangle((Width - Rectsize)/2,Height - Rectsize,5,5);
rect[7] = new Rectangle(Width - Rectsize,Height - Rectsize,5,5);
} private void SetBindSize()
{
if(BindControl!=null)
{
BindControl.Width = this.Width - 2 * Rectsize ;
BindControl.Height = this.Height - 2 * Rectsize ;
FrontLabel.Size = BindControl.Size;
}
} private void TrackCom_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(ComDragFlag)
{
this.Left = ComStartPoint.X;
this.Top = ComStartPoint.Y;
ComDragFlag = false;
}
} private void TrackCom_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{ } private void ChangeWidth(int X)
{
int w = this.Width - ComStartPoint.X + X;
if(w>2*Rectsize && w<int.MaxValue)
{
this.Width = w;
ComStartPoint.X = X;
}
} private void ChangeHeight(int Y)
{
int h = this.Height - ComStartPoint.Y + Y;
if(h>2*Rectsize && h<int.MaxValue)
{
this.Height = h;
ComStartPoint.Y = Y;
}
} private void ChangeTop(int Y)
{
int h,b;
b = this.Bottom;
if((h = this.Top - ComStartPoint.Y + Y) < (b - 2*Rectsize))
{
this.Top = h;
this.Height = b - this.Top;
}
} private void ChangeLeft(int X)
{
int w,r;
r = this.Right;
if((w = this.Left - ComStartPoint.X + X) < (r - 2*Rectsize))
{
this.Left = w;
this.Width = r - this.Left;
}
}
private void TrackCom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ComStartPoint.X = e.X;
ComStartPoint.Y = e.Y;
ComDragFlag = true;
}
private void TrackCom_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(ComDragFlag)
{
switch(SelDragDirect)
{
case -1://整体拖动
this.Left = this.Left - ComStartPoint.X + e.X;
this.Top = this.Top - ComStartPoint.Y + e.Y;
break;
case 0://左上角拖动
ChangeLeft(e.X);
ChangeTop(e.Y);
break;
case 1://顶中部拖动
ChangeTop(e.Y);
break;
case 2://右上角拖动
ChangeWidth(e.X);
ChangeTop(e.Y);
break;
case 3://左中部拖动
ChangeLeft(e.X);
break;
case 4://右中部拖动
ChangeWidth(e.X);
break;
case 5://左下角拖动
ChangeLeft(e.X);
ChangeHeight(e.Y);
break;
case 6://底中部拖动
ChangeHeight(e.Y);
break;
case 7://右下角拖动
ChangeWidth(e.X);
ChangeHeight(e.Y);
break;
}
}
else
{
SelDragDirect=-1; // \\\\\\\\\\\\\\\
if(rect[0].Contains(e.X,e.Y))
{
SelDragDirect=0;
this.Cursor = Cursors.SizeNWSE;
return;
} // |||||||||||||||
if(rect[1].Contains(e.X,e.Y))
{
SelDragDirect=1;
this.Cursor = Cursors.SizeNS;
return;
}
// ////////////////
if(rect[2].Contains(e.X,e.Y))
{
SelDragDirect=2;
this.Cursor = Cursors.SizeNESW;
return;
} // ------------------
if(rect[3].Contains(e.X,e.Y))
{
SelDragDirect=3;
this.Cursor = Cursors.SizeWE;
return;
}
if(rect[4].Contains(e.X,e.Y))
{
SelDragDirect=4;
this.Cursor = Cursors.SizeWE;
return;
}
if(rect[5].Contains(e.X,e.Y))
{
SelDragDirect=5;
this.Cursor = Cursors.SizeNESW;
return;
}
if(rect[6].Contains(e.X,e.Y))
{
SelDragDirect=6;
this.Cursor = Cursors.SizeNS;
return;
}
if(rect[7].Contains(e.X,e.Y))
{
SelDragDirect=7;
this.Cursor = Cursors.SizeNWSE;
return;
}
this.Cursor = Cursors.SizeAll;
}
} private void TrackCom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(ComDragFlag)
{
ComDragFlag = false;
}
} private void TrackCom_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics; Pen pen = new Pen(Color.Gold,1);
Brush brush = new SolidBrush(Color.Blue); for(int i=0;i<8;i++)
{
g.FillRectangle(brush,rect[i]);
g.DrawRectangle(pen,rect[i]);
}
} private void TrackCom_Move(object sender, System.EventArgs e)
{
SetBindSize();
} private void TrackCom_Resize(object sender, System.EventArgs e)
{
SetRect();
SetBindSize();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; namespace WindowsApplication4
{
public class TrackCom : System.Windows.Forms.Label
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private const int Rectsize = 6;
private Point ComStartPoint;
private bool ComDragFlag;
private Control BindControl;
private Rectangle[] rect;
private int SelDragDirect;
private Label FrontLabel; public TrackCom(Control cont)
{
// This call is required by the Windows.Forms Form Designer. this.Parent = cont.Parent;
this.Top = cont.Top - Rectsize ;
this.Left = cont.Left - Rectsize ;
this.Width = cont.Width + 2 * Rectsize ;
this.Height = cont.Height + 2 * Rectsize ;
this.BringToFront();
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.TrackCom_Paint);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown);
this.Move += new System.EventHandler(this.TrackCom_Move);
this.Resize += new System.EventHandler(this.TrackCom_Resize); FrontLabel = new Label();
FrontLabel.Parent = cont;
FrontLabel.BackColor = System.Drawing.Color.Transparent;
FrontLabel.Size = cont.Size;
FrontLabel.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
FrontLabel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
FrontLabel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
FrontLabel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
FrontLabel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown); BindControl = cont;
BindControl.Parent = this;
BindControl.Top = Rectsize ;
BindControl.Left = Rectsize ;
SetRect();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing)
{
BindControl.Parent = this.Parent;
BindControl.Top = this.Top + Rectsize ;
BindControl.Left = this.Left + Rectsize ;
FrontLabel.Dispose();
if( disposing )
{
if(components != null)
{ components.Dispose();
}
}
base.Dispose( disposing );
} private void SetRect()
{
rect = new Rectangle[8];
rect[0] = new Rectangle(0,0,5,5);
rect[1] = new Rectangle((Width - Rectsize)/2,0,5,5);
rect[2] = new Rectangle(Width - Rectsize,0,5,5);
rect[3] = new Rectangle(0,(Height - Rectsize)/2,5,5);
rect[4] = new Rectangle(Width - Rectsize,(Height - Rectsize)/2,5,5);
rect[5] = new Rectangle(0,Height - Rectsize,5,5);
rect[6] = new Rectangle((Width - Rectsize)/2,Height - Rectsize,5,5);
rect[7] = new Rectangle(Width - Rectsize,Height - Rectsize,5,5);
} private void SetBindSize()
{
if(BindControl!=null)
{
BindControl.Width = this.Width - 2 * Rectsize ;
BindControl.Height = this.Height - 2 * Rectsize ;
FrontLabel.Size = BindControl.Size;
}
} private void TrackCom_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(ComDragFlag)
{
this.Left = ComStartPoint.X;
this.Top = ComStartPoint.Y;
ComDragFlag = false;
}
} private void TrackCom_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{ } private void ChangeWidth(int X)
{
int w = this.Width - ComStartPoint.X + X;
if(w>2*Rectsize && w<int.MaxValue)
{
this.Width = w;
ComStartPoint.X = X;
}
} private void ChangeHeight(int Y)
{
int h = this.Height - ComStartPoint.Y + Y;
if(h>2*Rectsize && h<int.MaxValue)
{
this.Height = h;
ComStartPoint.Y = Y;
}
} private void ChangeTop(int Y)
{
int h,b;
b = this.Bottom;
if((h = this.Top - ComStartPoint.Y + Y) < (b - 2*Rectsize))
{
this.Top = h;
this.Height = b - this.Top;
}
} private void ChangeLeft(int X)
{
int w,r;
r = this.Right;
if((w = this.Left - ComStartPoint.X + X) < (r - 2*Rectsize))
{
this.Left = w;
this.Width = r - this.Left;
}
}
private void TrackCom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ComStartPoint.X = e.X;
ComStartPoint.Y = e.Y;
ComDragFlag = true;
}
private void TrackCom_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(ComDragFlag)
{
switch(SelDragDirect)
{
case -1://整体拖动
this.Left = this.Left - ComStartPoint.X + e.X;
this.Top = this.Top - ComStartPoint.Y + e.Y;
break;
case 0://左上角拖动
ChangeLeft(e.X);
ChangeTop(e.Y);
break;
case 1://顶中部拖动
ChangeTop(e.Y);
break;
case 2://右上角拖动
ChangeWidth(e.X);
ChangeTop(e.Y);
break;
case 3://左中部拖动
ChangeLeft(e.X);
break;
case 4://右中部拖动
ChangeWidth(e.X);
break;
case 5://左下角拖动
ChangeLeft(e.X);
ChangeHeight(e.Y);
break;
case 6://底中部拖动
ChangeHeight(e.Y);
break;
case 7://右下角拖动
ChangeWidth(e.X);
ChangeHeight(e.Y);
break;
}
}
else
{
SelDragDirect=-1; // \\\\\\\\\\\\\\\
if(rect[0].Contains(e.X,e.Y))
{
SelDragDirect=0;
this.Cursor = Cursors.SizeNWSE;
return;
} // |||||||||||||||
if(rect[1].Contains(e.X,e.Y))
{
SelDragDirect=1;
this.Cursor = Cursors.SizeNS;
return;
}
// ////////////////
if(rect[2].Contains(e.X,e.Y))
{
SelDragDirect=2;
this.Cursor = Cursors.SizeNESW;
return;
} // ------------------
if(rect[3].Contains(e.X,e.Y))
{
SelDragDirect=3;
this.Cursor = Cursors.SizeWE;
return;
}
if(rect[4].Contains(e.X,e.Y))
{
SelDragDirect=4;
this.Cursor = Cursors.SizeWE;
return;
}
if(rect[5].Contains(e.X,e.Y))
{
SelDragDirect=5;
this.Cursor = Cursors.SizeNESW;
return;
}
if(rect[6].Contains(e.X,e.Y))
{
SelDragDirect=6;
this.Cursor = Cursors.SizeNS;
return;
}
if(rect[7].Contains(e.X,e.Y))
{
SelDragDirect=7;
this.Cursor = Cursors.SizeNWSE;
return;
}
this.Cursor = Cursors.SizeAll;
}
} private void TrackCom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(ComDragFlag)
{
ComDragFlag = false;
}
} private void TrackCom_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics; Pen pen = new Pen(Color.Gold,1);
Brush brush = new SolidBrush(Color.Blue); for(int i=0;i<8;i++)
{
g.FillRectangle(brush,rect[i]);
g.DrawRectangle(pen,rect[i]);
}
} private void TrackCom_Move(object sender, System.EventArgs e)
{
SetBindSize();
} private void TrackCom_Resize(object sender, System.EventArgs e)
{
SetRect();
SetBindSize();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货