你还要添加如下几个文(文件)类://对象集合定义 using System; using System.Collections.Generic; using System.Text; using DrawLines.DrawObjs; using System.Windows.Forms; using System.Drawing;namespace DrawLines { class DrawList : List<DrawBase> { private Control m_Owner; public DrawList(Control owner) { this.m_Owner = owner; } internal DrawBase GetNear(int x, int y) { foreach (DrawBase draw in this) { if (draw.Near(x, y)) { return draw; } } return null; } internal void Draw(Graphics graphics) { foreach (DrawBase draw in this) { draw.Draw(graphics); } } } }//对象基类定义 using System; using System.Collections.Generic; using System.Text; using System.Drawing;namespace DrawLines.DrawObjs { abstract class DrawBase { internal Color m_BackColor; internal Color m_ForeColor; internal static int m_HalfGrab; public static int HalfGrab { get { return DrawBase.m_HalfGrab; } set { DrawBase.m_HalfGrab = value; } } public Color BackColor { get { return m_BackColor; } set { m_BackColor = value; } } public Color ForeColor { get { return m_ForeColor; } set { m_ForeColor = value; } } public abstract Rectangle GetBound(); public abstract void Draw(Graphics g); public abstract bool Near(int x, int y); public abstract void SetBound(Rectangle bound); } }//线定义 using System; using System.Collections.Generic; using System.Text; using System.Drawing;namespace DrawLines.DrawObjs { class LineObj : DrawBase { private Point m_Start; private Point m_End; public LineObj(Point start, Point end) { this.m_Start = start; this.m_End = end; } public override System.Drawing.Rectangle GetBound() { int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X; int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y; int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X; int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } public override void Draw(System.Drawing.Graphics g) { using (Pen pen = new Pen(this.m_ForeColor)) { g.DrawLine(pen, this.m_Start, this.m_End); } } public override bool Near(int x, int y) { //点到直线的距离是否在抓取范围之内 float A = this.m_End.Y - this.m_Start.Y; float B = this.m_End.X - this.m_Start.X; float C = B * this.m_Start.Y - A * this.m_Start.X; double D = (A * x - B * y + C) / (Math.Sqrt(A * A + B * B)); if (D >= -m_HalfGrab && D <= m_HalfGrab) { RectangleF bounds = this.GetBound(); bounds.Inflate(m_HalfGrab, m_HalfGrab); return bounds.Contains(x, y); } return false; } public override void SetBound(Rectangle bound) { this.m_Start = new Point(bound.X, bound.Y); this.m_End = new Point(bound.Right, bound.Bottom); } } }//矩形定义 using System; using System.Collections.Generic; using System.Text; using System.Drawing;namespace DrawLines.DrawObjs { class RectangleObj : DrawBase { private Point m_Start; private Point m_End; private bool m_Solid; public RectangleObj(Point start, Point end) { this.m_Start = start; this.m_End = end; } public bool Solid { get { return m_Solid; } set { m_Solid = value; } } public override System.Drawing.Rectangle GetBound() { int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X; int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y; int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X; int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } public override void Draw(Graphics g) { Rectangle bound = this.GetBound(); if (this.m_Solid) { using (SolidBrush brush = new SolidBrush(this.m_BackColor)) { g.FillRectangle(brush, bound); } } using (Pen pen = new Pen(this.m_ForeColor)) { g.DrawRectangle(pen, bound); } } public override bool Near(int x, int y) { Rectangle bound = this.GetBound(); Rectangle inner = bound; Rectangle outer = bound; inner.Inflate(-m_HalfGrab, -m_HalfGrab); outer.Inflate(m_HalfGrab, m_HalfGrab); Region reg = new Region(outer); reg.Exclude(inner); return reg.IsVisible(x, y); } public override void SetBound(Rectangle bound) { this.m_Start = new Point(bound.X, bound.Y); this.m_End = new Point(bound.Right, bound.Bottom); } } }//按钮定义 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms;namespace DrawLines.DrawObjs { class ButtonObj:DrawBase { private Point m_Start; private Point m_End; private string m_Text; public ButtonObj(Point start, Point end) { this.m_Start = start; this.m_End = end; } public string Text { get { return m_Text; } set { m_Text = value; } } public override System.Drawing.Rectangle GetBound() { int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X; int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y; int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X; int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } public override void Draw(Graphics g) { Rectangle bound = this.GetBound(); using (Pen pen = new Pen(this.m_ForeColor)) { ControlPaint.DrawButton(g, bound, ButtonState.Normal); using (SolidBrush brush = new SolidBrush(this.m_ForeColor)) { using (Font font = new Font("宋体", 10)) { using (StringFormat format = new StringFormat()) { format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; g.DrawString(this.m_Text, font, brush, bound, format); } } } } } public override bool Near(int x, int y) { Rectangle bound = this.GetBound(); Rectangle outer = bound; outer.Inflate(m_HalfGrab, m_HalfGrab); return outer.Contains(x, y); } public override void SetBound(Rectangle bound) { this.m_Start = new Point(bound.X, bound.Y); this.m_End = new Point(bound.Right, bound.Bottom); } } }
this.Cursor = new Cursor("string fileName");
这是我的代码,
private void Form1_Paint(object sender, PaintEventArgs e)
{
int point1 = 150;//基准点x
int point2 = 30;//基准点y
int jlwidh = 90;//外眶底部离边的距离Y
int jlhight = 300;//外眶右部离边的距离X
Graphics gb = this.CreateGraphics();
gb.Clear(Color.Black);
Pen p = new Pen(Color.FromArgb(60, 255, 0, 0), Width = 1);//定义画笔的宽度颜色等属性
Pen p1 = new Pen(Color.Red, Width = 1);
Pen p2 = new Pen(Color.Green, Width = 1);
Pen p3 = new Pen(Color.Red, Width = 3);
//绘制外眶 int stup = (this.Height - jlwidh - point2) / 35;//划分x轴步长
gb.DrawRectangle(p1, point1, point2, this.Width - jlhight, this.Height - jlwidh - point2 );//画出外眶 //平分 for (int i = point2 + stup; i < this.Height - jlwidh + point2; i = i + stup)
gb.DrawLine(p, point1, i - stup, this.Width - jlhight + point1, i - stup);
int r = 36;
int r1 = point2 + stup;
while (r1 < (this.Height - jlwidh + point2 + stup - 20))
{ gb.DrawString(r.ToString(), new Font("宋体", 11), Brushes.White, point1 - 20, r1 - stup - 7);//给出数标值 r1 = r1 + stup;
r--;
}
}
用控件实现这个效果,闪的历害.可能是移动时触发PAINT事件private void Form1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Left = e.X-2;
pictureBox2.Top = e .Y -2; }
就是有点长:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrawLines.DrawObjs;namespace DrawLines
{
public partial class Form1 : Form
{
private DrawList m_drawList;
private DrawBase m_curDraw;
private Rectangle m_curBound;
private Rectangle m_lastBound;
private Point m_StartPoint;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); this.InitObjs();
}
/// <summary>
/// 构造对象
/// </summary>
private void InitObjs()
{
this.m_drawList = new DrawList(this); DrawBase.HalfGrab = 5;
LineObj line0 = new LineObj(new Point(10, 10), new Point(100, 100));
line0.ForeColor = Color.Red; LineObj line1 = new LineObj(new Point(16, 67), new Point(100, 180));
line1.ForeColor = Color.Blue; RectangleObj rect = new RectangleObj(new Point(120, 70), new Point(220, 200));
rect.ForeColor = Color.Green; ButtonObj button = new ButtonObj(new Point(20, 20), new Point(90, 45));
button.ForeColor = Color.Black;
button.Text = "button1";
m_drawList.Add(line0);
m_drawList.Add(line1);
m_drawList.Add(rect);
m_drawList.Add(button);
}
/// <summary>
/// 绘制对象
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.m_drawList.Draw(e.Graphics);
}
/// <summary>
/// 重写以移动对象或设置鼠标
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Capture)
{
if (this.m_curDraw != null)
{
this.moveObj(e);
}
return;
}
if (this.m_drawList.GetNear(e.X, e.Y) != null)
{
this.Cursor = Cursors.SizeAll;
}
else
{
this.Cursor = Cursors.Default;
}
}
/// <summary>
/// 重写以获取鼠标下的对象
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.getObj(e);
}
/// <summary>
/// 重写以按Delete键删除当前对象
/// </summary>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Delete)
{
if (this.m_curDraw != null)
{
this.deleteObj();
}
}
return base.ProcessDialogKey(keyData);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.m_curDraw != null)
{
this.deleteObj();
}
else
{
MessageBox.Show(this, "没有选中的对象,请使用鼠标点选择一个!", "提示");
}
}
private void moveObj(MouseEventArgs e)
{
Rectangle bound = this.m_curBound; bound.Offset(e.X - this.m_StartPoint.X, e.Y - this.m_StartPoint.Y);
this.m_curDraw.SetBound(bound); Rectangle _last = this.m_lastBound;
Rectangle _bound = bound;
_last.Inflate(2, 2);
_bound.Inflate(2, 2); using (Region invReg = new Region(_last))
{
invReg.Union(_bound);
this.Invalidate(invReg);
}
this.m_lastBound = bound;
}
private void getObj(MouseEventArgs e)
{
this.m_curDraw = this.m_drawList.GetNear(e.X, e.Y);
if (this.m_curDraw != null)
{
this.m_curBound = this.m_curDraw.GetBound();
this.m_StartPoint = e.Location;
}
}
private void deleteObj()
{
Rectangle bound = this.m_curDraw.GetBound();
this.m_drawList.Remove(this.m_curDraw);
this.m_curDraw = null;
bound.Inflate(2, 2);
this.Invalidate(bound);
}
}
}
这一句提示找不到
using System;
using System.Collections.Generic;
using System.Text;
using DrawLines.DrawObjs;
using System.Windows.Forms;
using System.Drawing;namespace DrawLines
{
class DrawList : List<DrawBase>
{
private Control m_Owner;
public DrawList(Control owner)
{
this.m_Owner = owner;
}
internal DrawBase GetNear(int x, int y)
{
foreach (DrawBase draw in this)
{
if (draw.Near(x, y))
{
return draw;
}
}
return null;
} internal void Draw(Graphics graphics)
{
foreach (DrawBase draw in this)
{
draw.Draw(graphics);
}
}
}
}//对象基类定义
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;namespace DrawLines.DrawObjs
{
abstract class DrawBase
{
internal Color m_BackColor;
internal Color m_ForeColor;
internal static int m_HalfGrab; public static int HalfGrab
{
get { return DrawBase.m_HalfGrab; }
set { DrawBase.m_HalfGrab = value; }
} public Color BackColor
{
get { return m_BackColor; }
set { m_BackColor = value; }
} public Color ForeColor
{
get { return m_ForeColor; }
set { m_ForeColor = value; }
} public abstract Rectangle GetBound();
public abstract void Draw(Graphics g);
public abstract bool Near(int x, int y);
public abstract void SetBound(Rectangle bound);
}
}//线定义
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;namespace DrawLines.DrawObjs
{
class LineObj : DrawBase
{
private Point m_Start;
private Point m_End;
public LineObj(Point start, Point end)
{
this.m_Start = start;
this.m_End = end;
}
public override System.Drawing.Rectangle GetBound()
{
int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
return Rectangle.FromLTRB(x, y, r, b);
} public override void Draw(System.Drawing.Graphics g)
{
using (Pen pen = new Pen(this.m_ForeColor))
{
g.DrawLine(pen, this.m_Start, this.m_End);
}
} public override bool Near(int x, int y)
{
//点到直线的距离是否在抓取范围之内
float A = this.m_End.Y - this.m_Start.Y;
float B = this.m_End.X - this.m_Start.X;
float C = B * this.m_Start.Y - A * this.m_Start.X;
double D = (A * x - B * y + C) / (Math.Sqrt(A * A + B * B));
if (D >= -m_HalfGrab && D <= m_HalfGrab)
{
RectangleF bounds = this.GetBound();
bounds.Inflate(m_HalfGrab, m_HalfGrab);
return bounds.Contains(x, y);
}
return false;
} public override void SetBound(Rectangle bound)
{
this.m_Start = new Point(bound.X, bound.Y);
this.m_End = new Point(bound.Right, bound.Bottom);
}
}
}//矩形定义
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;namespace DrawLines.DrawObjs
{
class RectangleObj : DrawBase
{
private Point m_Start;
private Point m_End;
private bool m_Solid;
public RectangleObj(Point start, Point end)
{
this.m_Start = start;
this.m_End = end;
}
public bool Solid
{
get { return m_Solid; }
set { m_Solid = value; }
}
public override System.Drawing.Rectangle GetBound()
{
int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
return Rectangle.FromLTRB(x, y, r, b);
} public override void Draw(Graphics g)
{
Rectangle bound = this.GetBound();
if (this.m_Solid)
{
using (SolidBrush brush = new SolidBrush(this.m_BackColor))
{
g.FillRectangle(brush, bound);
}
}
using (Pen pen = new Pen(this.m_ForeColor))
{
g.DrawRectangle(pen, bound);
}
} public override bool Near(int x, int y)
{
Rectangle bound = this.GetBound();
Rectangle inner = bound;
Rectangle outer = bound;
inner.Inflate(-m_HalfGrab, -m_HalfGrab);
outer.Inflate(m_HalfGrab, m_HalfGrab);
Region reg = new Region(outer);
reg.Exclude(inner);
return reg.IsVisible(x, y);
} public override void SetBound(Rectangle bound)
{
this.m_Start = new Point(bound.X, bound.Y);
this.m_End = new Point(bound.Right, bound.Bottom);
}
}
}//按钮定义
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;namespace DrawLines.DrawObjs
{
class ButtonObj:DrawBase
{
private Point m_Start;
private Point m_End;
private string m_Text;
public ButtonObj(Point start, Point end)
{
this.m_Start = start;
this.m_End = end;
}
public string Text
{
get { return m_Text; }
set { m_Text = value; }
}
public override System.Drawing.Rectangle GetBound()
{
int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
return Rectangle.FromLTRB(x, y, r, b);
} public override void Draw(Graphics g)
{
Rectangle bound = this.GetBound();
using (Pen pen = new Pen(this.m_ForeColor))
{
ControlPaint.DrawButton(g, bound, ButtonState.Normal);
using (SolidBrush brush = new SolidBrush(this.m_ForeColor))
{
using (Font font = new Font("宋体", 10))
{
using (StringFormat format = new StringFormat())
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.DrawString(this.m_Text, font, brush, bound, format);
}
}
}
}
} public override bool Near(int x, int y)
{
Rectangle bound = this.GetBound();
Rectangle outer = bound;
outer.Inflate(m_HalfGrab, m_HalfGrab);
return outer.Contains(x, y);
} public override void SetBound(Rectangle bound)
{
this.m_Start = new Point(bound.X, bound.Y);
this.m_End = new Point(bound.Right, bound.Bottom);
}
}
}
我运行成功
现在我研究一下代码,看看如何运用到我的上.
等一会回来结贴.