C# 如何移动通过绘图创建的线并调整其大小?
我用这个代码在面板上画了一条线C# 如何移动通过绘图创建的线并调整其大小?,c#,visual-studio-2010,C#,Visual Studio 2010,我用这个代码在面板上画了一条线 private bool isMoving = false; private Point mouseDownPosition = Point.Empty; private Point mouseMovePosition = Point.Empty; private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>&g
private bool isMoving = false;
private Point mouseDownPosition = Point.Empty;
private Point mouseMovePosition = Point.Empty;
private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
private void PanelPaint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
Pen p = new Pen(Color.Red, 7);
if (isMoving)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(panel1.BackColor);
g.DrawLine(p, mouseDownPosition, mouseMovePosition);
foreach (var line in lines)
{
g.DrawLine(p, line.Item1, line.Item2);
}
}
}
private void PanelMouseDown(object sender, MouseEventArgs e)
{
isMoving = true;
mouseDownPosition = e.Location;
}
private void PanelMouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
mouseMovePosition = e.Location;
panel1.Invalidate();
}
}
private void PanelMouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
}
isMoving = false;
}
private bool isMoving=false;
私有点mouseDownPosition=Point.Empty;
私有点mouseMovePosition=点.空;
私有列表行=新列表();
私有void PanelPaint(对象发送器、PaintEventArgs e)
{
var g=e.图形;
Pen p=新笔(颜色:红色,7);
如果(正在移动)
{
e、 Graphics.SmoothingMode=SmoothingMode.AntiAlias;
g、 清晰(面板1.背景色);
g、 抽绳(p、鼠标向下位置、鼠标移动位置);
foreach(行中的var行)
{
g、 抽绳(p,第1行,第2行);
}
}
}
私有void panel mousedown(对象发送器,MouseEventArgs e)
{
isMoving=真;
mouseDownPosition=e.位置;
}
private void PanelMouseMove(对象发送器,MouseEventArgs e)
{
如果(正在移动)
{
mouseMovePosition=e.位置;
1.使无效();
}
}
私有void PanelMouseUp(对象发送器,MouseEventArgs e)
{
如果(正在移动)
{
添加(Tuple.Create(mouseDownPosition,mouseMovePosition));
}
isMoving=假;
}
然后我需要能够调整大小和移动鼠标线,但我不知道如何才能做到这一点
如果您对此有任何想法或指导,我将不胜感激
谢谢大家。
public class LineMover : Form
{
public LineMover()
{
this.DoubleBuffered = true;
this.Paint += new PaintEventHandler(LineMover_Paint);
this.MouseMove += new MouseEventHandler(LineMover_MouseMove);
this.MouseDown += new MouseEventHandler(LineMover_MouseDown);
this.MouseUp += new MouseEventHandler(LineMover_MouseUp);
this.Lines = new List<GraphLine>()
{
new GraphLine (10, 10, 100, 200),
new GraphLine (10, 150, 120, 40),
};
}
void LineMover_MouseUp(object sender, MouseEventArgs e)
{
if (Moving != null)
{
this.Capture = false;
Moving = null;
}
RefreshLineSelection(e.Location);
}
void LineMover_MouseDown(object sender, MouseEventArgs e)
{
RefreshLineSelection(e.Location);
if (this.SelectedLine != null && Moving == null)
{
this.Capture = true;
Moving = new MoveInfo
{
Line = this.SelectedLine,
StartLinePoint = SelectedLine.StartPoint,
EndLinePoint = SelectedLine.EndPoint,
StartMoveMousePoint = e.Location
};
}
RefreshLineSelection(e.Location);
}
void LineMover_Paint(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
foreach (var line in Lines)
{
var color = line == SelectedLine ? Color.Red : Color.Black;
var pen = new Pen(color, 2);
e.Graphics.DrawLine(pen, line.StartPoint, line.EndPoint);
}
}
void LineMover_MouseMove(object sender, MouseEventArgs e)
{
if (Moving != null)
{
Moving.Line.StartPoint = new PointF(Moving.StartLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.StartLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y);
Moving.Line.EndPoint = new PointF(Moving.EndLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.EndLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y);
}
RefreshLineSelection(e.Location);
}
private void RefreshLineSelection(Point point)
{
var selectedLine = FindLineByPoint(Lines, point);
if (selectedLine != this.SelectedLine)
{
this.SelectedLine = selectedLine;
this.Invalidate();
}
if (Moving != null)
this.Invalidate();
this.Cursor =
Moving != null ? Cursors.Hand :
SelectedLine != null ? Cursors.SizeAll :
Cursors.Default;
}
public List<GraphLine> Lines = new List<GraphLine>();
GraphLine SelectedLine = null;
MoveInfo Moving = null;
static GraphLine FindLineByPoint(List<GraphLine> lines, Point p)
{
var size = 10;
var buffer = new Bitmap(size * 2, size * 2);
foreach (var line in lines)
{
//draw each line on small region around current point p and check pixel in point p
using (var g = Graphics.FromImage(buffer))
{
g.Clear(Color.Black);
g.DrawLine(new Pen(Color.Green, 3), line.StartPoint.X - p.X + size, line.StartPoint.Y - p.Y + size, line.EndPoint.X - p.X + size, line.EndPoint.Y - p.Y + size);
}
if (buffer.GetPixel(size, size).ToArgb() != Color.Black.ToArgb())
return line;
}
return null;
}
public static void Main()
{
Application.Run(new LineMover());
}
}
public class MoveInfo
{
public GraphLine Line;
public PointF StartLinePoint;
public PointF EndLinePoint;
public Point StartMoveMousePoint;
}
public class GraphLine
{
public GraphLine(float x1, float y1, float x2, float y2)
{
this.StartPoint = new PointF(x1, y1);
this.EndPoint = new PointF(x2, y2);
}
public PointF StartPoint;
public PointF EndPoint;
}
公共类LineMover:表单
{
公共线路移动器()
{
this.DoubleBuffered=true;
this.Paint+=新的PaintEventHandler(LineMover\u Paint);
this.MouseMove+=新的MouseEventHandler(LineMover\u MouseMove);
this.MouseDown+=新的MouseEventHandler(LineMover\u MouseDown);
this.MouseUp+=新的MouseEventHandler(LineMover\u MouseUp);
this.Lines=新列表()
{
新GraphLine(10,10,100,200),
新GraphLine(1015012040),
};
}
void LineMover_MouseUp(对象发送器,MouseEventArgs e)
{
如果(正在移动!=null)
{
这个.Capture=false;
移动=空;
}
刷新线路选择(如位置);
}
void LineMover\u MouseDown(对象发送器,MouseEventArgs e)
{
刷新线路选择(如位置);
if(this.SelectedLine!=null&&Moving==null)
{
这个.Capture=true;
移动=新移动信息
{
Line=this.SelectedLine,
StartLinePoint=SelectedLine.StartPoint,
EndLinePoint=SelectedLine.EndPoint,
StartMoveMousePoint=e.位置
};
}
刷新线路选择(如位置);
}
void LineMover_Paint(对象发送器,PaintEventArgs e)
{
e、 Graphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
e、 Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
foreach(行中的var行)
{
var color=line==SelectedLine?color.Red:color.Black;
var笔=新笔(颜色,2);
e、 绘图线(笔、线、起点、线、终点);
}
}
void LineMover\u MouseMove(对象发送器,MouseEventArgs e)
{
如果(正在移动!=null)
{
Moving.Line.StartPoint=新点F(Moving.StartLinePoint.X+e.X-Moving.StartMoveMousePoint.X,Moving.StartLinePoint.Y+e.Y-Moving.startMousePoint.Y);
Moving.Line.EndPoint=新点F(Moving.EndLinePoint.X+e.X-Moving.StartMoveMousePoint.X,Moving.EndLinePoint.Y+e.Y-Moving.StartMoveMousePoint.Y);
}
刷新线路选择(如位置);
}
专用空心刷新线选择(点)
{
var selectedLine=FindLineByPoint(线,点);
if(selectedLine!=此.selectedLine)
{
this.SelectedLine=SelectedLine;
这个。使无效();
}
如果(正在移动!=null)
这个。使无效();
这是光标=
移动!=空?光标。手:
SelectedLine!=null?Cursors.SizeAll:
游标。默认值;
}
公共列表行=新列表();
GraphLine SelectedLine=null;
MoveInfo Moving=null;
静态图形线FindLineByPoint(列表线,点p)
{
变量大小=10;
var buffer=新位图(大小*2,大小*2);
foreach(行中的var行)
{
//在当前点p周围的小区域上绘制每条线,并检查点p中的像素
使用(var g=Graphics.FromImage(缓冲区))
{
g、 清晰(颜色为黑色);
g、 DrawLine(新钢笔(颜色为绿色,3),line.StartPoint.X-p.X+尺寸,line.StartPoint.Y-p.Y+尺寸,line.EndPoint.X-p.X+尺寸,line.EndPoint.Y-p.Y+尺寸);
}
if(buffer.GetPixel(size,size.ToArgb()!=Color.Black.ToArgb())
回流线;
}
返回null;
}
公共静态void Main()
{
运行(newlinemover());
}
}
公共类移动信息
{
公共石墨线;
公共点;公共点;
公共点F端点;
公共点开始移动鼠标点;
}
公共类石墨线
{
公共图形线(浮点x1、浮点y1、浮点x2、浮点y2)
{
this.StartPoint=新的点f(x1,y1);
this.EndPoint=新的点f(x2,y2);
}
公共点;起点;
公共点F端点;
}
作者:黑格雷。在链接 也许我应该用图纸以外的东西来画这条线?