请教高手,我想要做一个随鼠标移动的"十"形线
我在FORM上已经画好网络络线,且根据数据生成各种图形.
现在想做一个随鼠标移动的 "十 "形线,有点象K线图里的效果.
如果用画线实现这个效果,我不知如何删除以前的线又不影响想要的图形.
请大家指点,最好能给上代码.
[解决办法]
你还要添加如下几个文(文件)类:
//对象集合定义
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);
}
}
}