WPF:如何制作可编辑路径

发布于 2024-10-20 21:43:20 字数 163 浏览 1 评论 0原文

我想知道是否有人可以指导我解决这个问题:

我需要通过单击画布上的几个点来创建一条路径,并且这些点将添加到路径几何图形中。完成路径后,用户可以“滑动”或移动路径的控制顶点(锚点)来调整路径的形状。

我已经弄清楚如何绘制“套索”样式路径,但是如何允许用户选择并移动路径中的单个点???

I was wondering if someone could guide me on this problem :

I need to create a path by clicking several points on a canvas, and these points would be added to the path geometry. After finishing the path, the user can "Slide" or Move the Control Vertices (anchors) of the path to adjust the shape of the path.

I have figured out how to draw a "lasso" style path, but how do i allow the user to select and move a single point in the path ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-10-27 21:43:20

您必须开发一个数据结构来存储点数据,以便可以轻松查询和操作。 Path 对象本身可能就足够了,但请考虑将其包装在另一个对象中以呈现更特定于域的接口。

您必须检测画布中的鼠标事件并对路径中的所有顶点进行命中测试。

点击测试是一个函数,它为您提供对路径中最接近鼠标坐标的单个点的引用,或者如果单击距离任何点太远而被视为“,则为空”打”。您的命中测试函数成为一个低级构造,您可以从中构建更有趣的编辑操作。

例如,您可以为路径中的每个点存储一个 bool 来指示是否选择该点。当您按下按钮拖动鼠标时,您可以通过在前面提到的数据结构中偏移它们的数据来拖动所有选定的点。

You will have to develop a data structure to store the point data so that it can be easily queried and manipulated. The Path object may be sufficient for this itself, but consider wrapping it in another object to present a more domain specific interface.

You will have to detect mouse events in the Canvas and hit test for all the vertices in the Path.

A hit test is a function that gives you a reference to a single point in the path nearest to the coordinate of the mouse or null if the click was too far from any of the points to be considered a "hit". Your hit test function becomes a low level construct from which you can build the more interesting editing operations.

For instance, you can store a bool for each point in the path indicating whether the point is selected. When you drag the mouse with the button down, you can drag all the selected points by offsetting their data in the data structure mentioned earlier.

这个俗人 2024-10-27 21:43:20

我会尝试以下操作:

  1. 在 C# 中,有一个 ObservableCollection,或者可能是一个 PointCollection
  2. 从该集合中,通过数据绑定,我将绘制一条路径并以某种方式从集合中获取其几何图形;
  3. 在路径层本身之上,我将添加某种 ItemsContainer,将 ItemsTemplate 设置为 System.Windows.Controls.Primitives.Thumb(处理拖动的控件),其 ControlTemplate 的形状为Ellipse 和 DataTrigger 根据是否被选择而改变其外观。 ItemsSource 也将绑定到集合。

这样做时,您将对路径进行命中测试,例如,突出显示其点,这将打开 ItemsContainer 的可见性(从而打开命中测试)。

有了这些,您可以使用“Drag”、“MouseMove”等常规事件直接处理命中测试。

I would try the following:

  1. In C#, have an ObservableCollection<Point>, or perhaps a PointCollection.
  2. From that collection, via data-binding, I would draw a Path and get its geometry from the collection, somehow;
  3. Above the path layer itself, I would add an ItemsContainer of some sort, setting the ItemsTemplate to be a System.Windows.Controls.Primitives.Thumb (Control that handles dragging), with a ControlTemplate with shape of Ellipse and DataTrigger changing its appearence based on being sellected or not. ItemsSource will be bound to the collection, too.

Doing that, you'll hit-test the path, for example, to highlight its points, which will switch the ItemsContainer's visibility (and thus hit-testing) on.

With these, you can use regular events like "Drag", "MouseMove", etc. handling hit-test directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文