【发布时间】:2019-08-15 03:15:54
【问题描述】:
我有一个 txt 文件中的行列表,C# 程序需要从 txt 文件中收集数据并从 txt 文件中绘制每一行。我需要让它们以某种方式可点击,所以如果我点击一条线,程序会识别出特定的线对象是什么,我可以选择删除它并在没有那条线的情况下重新绘制图像(并重复此操作,直到我只有我的线需要)
我只是不确定如何实现这一点,我应该为可点击绘图部分使用什么技术。
我尝试使用 winform,因为它似乎适合带有画布和显示、绘图功能等的工作。我制作了一些类来存储来自 txt 的数据,并带有一个小的层次结构系统。上面有一个图像类,它有一个 Shape 列表(程序只处理一张图像,但图像可以包含更多形状,有些在另一个里面),Shape 类有一个 Line 列表,Line 类有 int 变量:从X,从Y,到X,到Y 我能够从 txt 中获取所有数据并加载到程序中,我将一些数据写入控制台,它们正确存储,我可以从 Image 对象中获取每条线的坐标。现在我需要一个一个地绘制每个线条对象并使它们可点击(可能是一个 onclick 事件,它返回或存储被点击的线条的数据)但是我被卡住的部分是我不知道我该怎么做计算点击是否在一条线上,如果是在一条线上,那么是哪个线对象来取回正确的数据。
/*
Example of input file (two triangle and one pentagon on the pic, lines with ? are shape dividers)
X:Y>X:Y
810:-448>935:-532
935:-532>806:-534
806:-534>804:-449
?:?>?:?
597:-529>673:-411
673:-411>747:-531
747:-531>597:-529
?:?>?:?
475:-275>582:-355
582:-355>541:-487
541:-487>411:-487
411:-487>370:-355
370:-355>475:-275
?:?>?:?
*/
//As I mentioned I stored the data in line objects
class Line
{
public int startX;
public int startY;
public int endX;
public int endY;
public Line(int startX, int startY, int endX, int endY)
{
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
}
}
//Every shape is an object with a list of the Line objects
class Shape
{
public List<Line> lines = new List<Line>();
public void AddLine(Line a)
{
this.lines.Add(a);
}
public void RemoveLine(Line a)
{
this.lines.Remove(a);
}
public void ResetShape()
{
this.lines.Clear();
}
public Line GetLine(int index)
{
return this.lines[index];
}
}
//Image class looks similar, it has a List of Shapes, the main difference is the constructor.
//The constructor get the file path as parameter, opens it, read and store data
//Split every line, get the coordinates, make line objects and add them to a temp Shape object's list
//When the read reach a divider it adds the temp Shape object to the Image's shape list
//Then reset the temp Shape object and continue to collect new lines untile the file reach the last line (which is a divider)
【问题讨论】:
-
基本上,你应该参考GDI+进行渲染,扩展你的
Shape和Line类来实现接收鼠标点击坐标和点击测试的功能,你将拥有一个全新的winform领域去探索。 -
周围有很多例子,例如How to drag and move shapes in C#或this one。
-
谢谢您,您提供的链接 Reza Aghaei 非常有用,非常感谢你们俩!我想我现在可以完成这项任务了:)