WPF:如何制作可编辑路径
我想知道是否有人可以指导我解决这个问题:
我需要通过单击画布上的几个点来创建一条路径,并且这些点将添加到路径几何图形中。完成路径后,用户可以“滑动”或移动路径的控制顶点(锚点)来调整路径的形状。
我已经弄清楚如何绘制“套索”样式路径,但是如何允许用户选择并移动路径中的单个点???
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须开发一个数据结构来存储点数据,以便可以轻松查询和操作。 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.我会尝试以下操作:
ObservableCollection
,或者可能是一个PointCollection
。这样做时,您将对路径进行命中测试,例如,突出显示其点,这将打开 ItemsContainer 的可见性(从而打开命中测试)。
有了这些,您可以使用“Drag”、“MouseMove”等常规事件直接处理命中测试。
I would try the following:
ObservableCollection<Point>
, or perhaps aPointCollection
.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.