|
|
|
Goals to demonstrate:
Steps:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;had been generated for you
private Point[] points = new Point[4];
public Point[] Points {
get {
return points;
}
set {
points = value;
Invalidate();
}
}
and initialization code to the constructor
points[0].X = 100; points[0].Y = 100; points[1].X = 100; points[1].Y = 200; points[2].X = 200; points[2].Y = 200; points[3].X = 200; points[3].Y = 100;
base.OnPaint(e);
GraphicsPath gp = new GraphicsPath();
gp.AddClosedCurve(points);
e.Graphics.DrawPath(Pens.Black, gp);
gp.Dispose();
for(int i = 0; i < 4; i++) {
e.Graphics.FillRectangle(Brushes.Red, Points[i].X - 5, Points[i].Y - 5, 10, 10);
}
private int x, y;
private int movePoint = -1;
protected override void OnMouseDown(MouseEventArgs e) {
x = e.X;
y = e.Y;
if(e.Button == MouseButtons.Left) {
movePoint = PointByXY(e.X, e.Y);
this.Capture = false;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e) {
if(movePoint >= 0 && e.Button == MouseButtons.Left) {
Points[movePoint].X = e.X;
Points[movePoint].Y = e.Y;
Invalidate();
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e) {
if(movePoint >= 0 && e.Button == MouseButtons.Left) {
Points[movePoint].X = e.X;
Points[movePoint].Y = e.Y;
movePoint = -1;
Invalidate();
}
base.OnMouseUp(e);
}
internal int PointByXY(int x, int y) {
for(int i = 0; i < points.Length; i++) {
if(x >= Points[i].X - 5 && x <= Points[i].X + 5 &&
y >= Points[i].Y - 5 && y <= Points[i].Y + 5) {
return i;
}
}
return -1;
}

public class MyEditor : UITypeEditor {
private IWindowsFormsEditorService edSvc = null;
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
if(context != null && context.Instance != null && provider != null) {
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if(edSvc != null) {
MyControl orgControl = (MyControl)context.Instance;
MyControl propertiesControl = new MyControl();
propertiesControl.Width = orgControl.Width;
propertiesControl.Height = orgControl.Height;
Array.Copy(orgControl.Points, propertiesControl.Points, 4);
edSvc.DropDownControl(propertiesControl);
return propertiesControl.Points;
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.DropDown;
}
}
[Browsable(true)]
[Category("Image")]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
private MyControl component; private int movePoint = -1;and initialization code
public override void Initialize(IComponent component) {
this.component = component as MyControl;
base.Initialize(component);
}
protected override void OnMouseDragBegin(int x, int y) {
if (MyControl.MouseButtons == MouseButtons.Left) {
Point p = component.PointToClient(new Point(x, y));
movePoint = component.PointByXY(p.X, p.Y);
if(movePoint >= 0) {
component.Capture = true;
}
}
if (movePoint < 0) {
base.OnMouseDragBegin(x, y);
}
}
protected override void OnMouseDragMove(int x, int y) {
if (movePoint >= 0) {
Point p = component.PointToClient(new Point(x, y));
component.Points[movePoint].X = p.X;
component.Points[movePoint].Y = p.Y;
component.Invalidate();
}
else {
base.OnMouseDragMove(x, y);
}
}
protected override void OnMouseDragEnd(bool cancel) {
if(movePoint >= 0) {
movePoint = -1;
component.Capture = false;
TypeDescriptor.GetProperties(component)["Points"].SetValue(component, component.Points);
}
else {
base.OnMouseDragEnd(cancel);
}
}
[Designer(typeof(MyDesigner))]
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if(sourceType == typeof(string)) {
return true;
}
else {
return base.CanConvertFrom(context, sourceType);
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if(destinationType == typeof(string)) {
return true;
}
else {
return base.CanConvertTo(context, destinationType);
}
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value) {
if(value is string) {
try {
string[] pointsString = ((string)value).Split(',');
List<Point> points = new List<Point>();
foreach(string s in pointsString) {
string tmp = s.TrimStart('(', ' ').TrimEnd(')', ' ');
string[] pointCoordinates = tmp.Split(';');
points.Add(new Point(int.Parse(pointCoordinates[0]), int.Parse(pointCoordinates[1])));
}
return points.ToArray();
}
catch(Exception ex) {
throw new FormatException(ex.Message, ex);
}
}
else {
return base.ConvertFrom(context, culture, value);
}
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType) {
if(destinationType == typeof(string)) {
try {
Point[] points = (Point[])value;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < points.Length; i++) {
sb.AppendFormat("({0}; {1})", points[i].X, points[i].Y);
if(i < points.Length - 1) {
sb.Append(',');
}
}
return sb.ToString();
}
catch(Exception ex) {
throw new FormatException(ex.Message, ex);
}
}
else {
return base.ConvertTo(context, culture, value, destinationType);
}
}
[TypeConverter(typeof(MyTypeConverter))]


[ToolboxBitmap(typeof(MyControl), "tool.bmp")]



