什么东西

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace ImageControls
{
/// <summary>
/// 控件
/// </summary>
public class ShapeEx : System.Windows.Forms.Control
{


#region 字段
/// <summary>
/// 必需的设计器变量。
/// </summary>
private Color _BorderColor = new Color(); //边框颜色
private Color _BackColor = new Color(); //背景颜色
private bool _ReSizeble; //是否可调整大小
private Point _SelfLocation = new Point();
private Point _MouseLocation = new Point();
private int _SelfWidth;
private int _SelfHeight;
private int _SelectSelctedIndex; //0-8,0:SizeAll

private Rectangle _rectLeftSelector = new Rectangle();
private Rectangle _rectTopSelector = new Rectangle();
private Rectangle _rectRightSelector = new Rectangle();
private Rectangle _rectBottomSelector = new Rectangle();
private Rectangle _rectLeftTopSelector = new Rectangle();
private Rectangle _rectRightTopSelector = new Rectangle();
private Rectangle _rectRightBottomSelector = new Rectangle();
private Rectangle _rectLeftBottomSelector = new Rectangle();

#region modify by anby
private int _ShapeExControlID; //子增长ID,用于和其他本类控件做区分
private int _ShapeExJoinID; //用于连接外部控件ID
private int _ShapeExIsFocus; //0表示没有获取焦点,1表示获取焦点
#endregion

private System.ComponentModel.Container components = null;
#endregion

#region 构造函数
public ShapeEx()
{

// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#endregion

#region 属性
[DefaultValue("Black"), Description("边框颜色"), Category("Appearance")]
public Color BorderColor
{
get
{
// Insert code here.
return _BorderColor;
}
set
{
_BorderColor = value;
this.Invalidate();
}
}

[Description("控件ID号,自增长"),Category("Appearance")]
public int ShapeExControlID
{
get {return _ShapeExControlID;}
set {_ShapeExControlID = value;}
}

[Description("用于连接外部控件ID"), Category("Appearance")]
public int ShapeExJoinID
{
get {return _ShapeExJoinID; }
set { _ShapeExJoinID = value; }
}

[DefaultValue("Control"), Description("背景颜色"), Category("Appearance")]
public override Color BackColor
{
get
{
// Insert code here.
return _BackColor;
}
set
{
_BackColor = value;
this.Invalidate();
}
}

[DefaultValue(false), Description("运行中控件大小是否可拖拽编辑"), Category("Behavior")]
public bool ReSizeble
{
get
{
// Insert code here.
return _ReSizeble;
}
set
{
_ReSizeble = value;
this.Invalidate();
}
}

[Description("控件选择区域"), Category("Behavior")]
public Rectangle SelectRectangle
{
get
{
Rectangle selectRectangler = new Rectangle();
selectRectangler.X = this.Location.X + 7;
selectRectangler.Y = this.Location.Y + 7;
selectRectangler.Height = this.Height - 15;
selectRectangler.Width = this.Width - 15;
return selectRectangler;
}

}


/// <summary>
/// 是否被获取焦点
/// </summary>
public int ShapeExIsFocus
{
get { return this._ShapeExIsFocus;}
set { _ShapeExIsFocus = value; }
}

#endregion

#region 私有方法

private void DrawSelector(Graphics graphics)
{
SolidBrush SelectorPen = new SolidBrush(Color.White);
Pen borderPen = new Pen(this._BorderColor, 1);
try
{
//实心

PointF[] LeftPoints = getPointF(0, this.Height / 2 - 3, 6, 6);
graphics.FillClosedCurve(SelectorPen, LeftPoints);

PointF[] TopPoints = getPointF(this.Width / 2 - 3, 0, 6, 6);
graphics.FillClosedCurve(SelectorPen, TopPoints);

PointF[] RightPoints = getPointF(this.Width - 7, this.Height / 2 - 3, 6, 6);
graphics.FillClosedCurve(SelectorPen, RightPoints);

PointF[] BottomPoints = getPointF(this.Width / 2 - 3, this.Height - 7, 6, 6);
graphics.FillClosedCurve(SelectorPen, BottomPoints);

PointF[] LeftTopPoints = getPointF(0, 0, 6, 6);
graphics.FillClosedCurve(SelectorPen, LeftTopPoints);

PointF[] RightTopPoints = getPointF(this.Width - 7, 0, 6, 6);
graphics.FillClosedCurve(SelectorPen, RightTopPoints);

PointF[] RightBottomPoints = getPointF(this.Width - 7, this.Height - 7, 6, 6);
graphics.FillClosedCurve(SelectorPen, RightBottomPoints);

PointF[] LeftBottomPoints = getPointF(0, this.Height - 7, 6, 6);
graphics.FillClosedCurve(SelectorPen, LeftBottomPoints);
//边框
_rectLeftSelector.X = 0;
_rectLeftSelector.Y = this.Height / 2 - 3;
_rectLeftSelector.Height = 6;
_rectLeftSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectLeftSelector);

_rectTopSelector.X = this.Width / 2 - 3;
_rectTopSelector.Y = 0;
_rectTopSelector.Height = 6;
_rectTopSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectTopSelector);

_rectRightSelector.X = this.Width - 7;
_rectRightSelector.Y = this.Height / 2 - 3;
_rectRightSelector.Height = 6;
_rectRightSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectRightSelector);

_rectBottomSelector.X = this.Width / 2 - 3;
_rectBottomSelector.Y = this.Height - 7;
_rectBottomSelector.Height = 6;
_rectBottomSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectBottomSelector);

_rectLeftTopSelector.X = 0;
_rectLeftTopSelector.Y = 0;
_rectLeftTopSelector.Width = 6;
_rectLeftTopSelector.Height = 6;
graphics.DrawRectangle(borderPen, _rectLeftTopSelector);

_rectRightTopSelector.X = this.Width - 7;
_rectRightTopSelector.Y = 0;
_rectRightTopSelector.Width = 6;
_rectRightTopSelector.Height = 6;
graphics.DrawRectangle(borderPen, _rectRightTopSelector);

_rectRightBottomSelector.X = this.Width - 7;
_rectRightBottomSelector.Y = this.Height - 7;
_rectRightBottomSelector.Width = 6;
_rectRightBottomSelector.Height = 6;
graphics.DrawRectangle(borderPen, _rectRightBottomSelector);

_rectLeftBottomSelector.X = 0;
_rectLeftBottomSelector.Y = this.Height - 7;
_rectLeftBottomSelector.Width = 6;
_rectLeftBottomSelector.Height = 6;
graphics.DrawRectangle(borderPen, _rectLeftBottomSelector);
}
catch (Exception E)
{
throw E;
}
finally
{
SelectorPen.Dispose();
borderPen.Dispose();
}

}

private void ReDrawControl(Graphics graphics)
{

try
{

//绘制背景
/*
graphics.Clear(this._BackColor);
SolidBrush backPen=new SolidBrush(this._BackColor);
PointF point1 = new PointF(1,1);
PointF point2 = new PointF(this.Width-2,1);
PointF point3 = new PointF(this.Width-2,this.Height-2);
PointF point4 = new PointF(1,this.Height-2);
PointF[] points = {point1, point2, point3, point4};
graphics.FillClosedCurve(backPen, points);
*/
//绘制边框
Rectangle rectBorder = new Rectangle();
Pen borderPen = new Pen(this._BorderColor, 1);
rectBorder.X = 7;
rectBorder.Y = 7;
rectBorder.Height = this.Height - 15;
rectBorder.Width = this.Width - 15;
graphics.DrawRectangle(borderPen, rectBorder);
//绘制编辑框
if (_ReSizeble)
{
DrawSelector(graphics);
}
}
catch (Exception E)
{
throw E;
}
finally
{
graphics.Dispose();
}
}

private PointF[] getPointF(int x, int y, int Width, int Height)
{
PointF point1 = new PointF(x, y);
PointF point2 = new PointF(x + Width, y);
PointF point3 = new PointF(x + Width, y + Height);
PointF point4 = new PointF(x, y + Height);
PointF[] points = { point1, point2, point3, point4 };
return points;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

#region 组件设计器生成的代码
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.Resize += new EventHandler(ShapeEx_Resize);
this.MouseDown += new MouseEventHandler(ShapeEx_MouseDown);
this.MouseMove += new MouseEventHandler(ShapeEx_MouseMove);
this.MouseLeave += new EventHandler(ShapeEx_MouseLeave);
this.MouseUp += new MouseEventHandler(ShapeEx_MouseUp);
this.GotFocus+=ShapeEx_GotFocus;
this._BorderColor = Color.Black;
this._BackColor = Color.FromName("Control");
this._ReSizeble = false;
this._SelectSelctedIndex = -1;
SetStyle(ControlStyles.SupportsTransparentBackColor
| ControlStyles.UserPaint
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.Opaque, true);
BackColor = Color.Transparent;


}

protected override void OnLocationChanged(EventArgs e)
{
// pick up the container's surface again.
Visible = false;
Visible = true;
}


private void ShapeEx_LostFocus(object sender, EventArgs e)
{
this._ShapeExIsFocus = 0;
}

private void ShapeEx_GotFocus(object sender, EventArgs e)
{
this._ShapeExIsFocus = 1;
}


#endregion

#region 事件
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
ReDrawControl(pe.Graphics);
}

private void ShapeEx_Resize(object sender, EventArgs e)
{
if (this.Width < 16 || this.Height < 16)
{
this.Width = 16;
this.Height = 16;
}
this.Invalidate();
}

private void ShapeEx_MouseDown(object sender, MouseEventArgs e)
{
this._ShapeExIsFocus = 1;
if (_ReSizeble)
{
if (_rectLeftSelector.Contains(e.X, e.Y) ||
_rectRightSelector.Contains(e.X, e.Y) ||
_rectTopSelector.Contains(e.X, e.Y) ||
_rectBottomSelector.Contains(e.X, e.Y) ||
_rectLeftTopSelector.Contains(e.X, e.Y) ||
_rectRightTopSelector.Contains(e.X, e.Y) ||
_rectRightBottomSelector.Contains(e.X, e.Y) ||
_rectLeftBottomSelector.Contains(e.X, e.Y))
{
if (_rectLeftTopSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNWSE;
this._SelectSelctedIndex = 1;
}

if (_rectTopSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNS;
this._SelectSelctedIndex = 2;
}
if (_rectRightTopSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNESW;
this._SelectSelctedIndex = 3;
}
if (_rectRightSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeWE;
this._SelectSelctedIndex = 4;
}
if (_rectRightBottomSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNWSE;
this._SelectSelctedIndex = 5;
}
if (_rectBottomSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNS;
this._SelectSelctedIndex = 6;
}
if (_rectLeftBottomSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeNESW;
this._SelectSelctedIndex = 7;
}
if (_rectLeftSelector.Contains(e.X, e.Y))
{
this.Cursor = Cursors.SizeWE;
this._SelectSelctedIndex = 8;
}

}
else
{
this.Cursor = Cursors.SizeAll;
this._SelectSelctedIndex = 0;
}
this._SelfLocation.X = this.Location.X;
this._SelfLocation.Y = this.Location.Y;
this._MouseLocation.X = Cursor.Position.X;
this._MouseLocation.Y = Cursor.Position.Y;
this._SelfWidth = this.Width;
this._SelfHeight = this.Height;
}
}

private void ShapeEx_MouseMove(object sender, MouseEventArgs e)
{
//move and resize
switch (this._SelectSelctedIndex)
{
case 0:
this.Location = new Point(Cursor.Position.X - (_MouseLocation.X - _SelfLocation.X), Cursor.Position.Y - (_MouseLocation.Y - _SelfLocation.Y));
break;
case 1:
this.Height = this._SelfHeight - (Cursor.Position.Y - _MouseLocation.Y);
this.Width = this._SelfWidth - (Cursor.Position.X - _MouseLocation.X);
this.Location = new Point(Cursor.Position.X - _MouseLocation.X + _SelfLocation.X, Cursor.Position.Y - _MouseLocation.Y + _SelfLocation.Y);
break;
case 2:
this.Height = this._SelfHeight - (Cursor.Position.Y - _MouseLocation.Y);
this.Location = new Point(_SelfLocation.X, Cursor.Position.Y - _MouseLocation.Y + _SelfLocation.Y);
break;
case 3:
this.Height = this._SelfHeight - (Cursor.Position.Y - _MouseLocation.Y);
this.Width = this._SelfWidth + (Cursor.Position.X - _MouseLocation.X);
this.Location = new Point(_SelfLocation.X, Cursor.Position.Y - (_MouseLocation.Y - _SelfLocation.Y));
break;
case 4:
this.Width = this._SelfWidth + (Cursor.Position.X - _MouseLocation.X);
break;
case 5:
this.Height = this._SelfHeight + (Cursor.Position.Y - _MouseLocation.Y);
this.Width = this._SelfWidth + (Cursor.Position.X - _MouseLocation.X);
break;
case 6:
this.Height = this._SelfHeight + (Cursor.Position.Y - _MouseLocation.Y);
break;
case 7:
this.Height = this._SelfHeight + (Cursor.Position.Y - _MouseLocation.Y);
this.Width = this._SelfWidth - (Cursor.Position.X - _MouseLocation.X);
this.Location = new Point(Cursor.Position.X - _MouseLocation.X + _SelfLocation.X, _SelfLocation.Y);
break;
case 8:
this.Width = this._SelfWidth - (Cursor.Position.X - _MouseLocation.X);
this.Location = new Point(Cursor.Position.X - _MouseLocation.X + _SelfLocation.X, _SelfLocation.Y);
break;
}

}

private void ShapeEx_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
this._SelectSelctedIndex = -1;
}

private void ShapeEx_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Default;
this._SelectSelctedIndex = -1;
}

#endregion

}
}

 

posted @ 2013-06-28 17:37  尼姑哪里跑  阅读(277)  评论(0编辑  收藏  举报