简体   繁体   English

使用WPF在Canvas上画线

[英]Drawing line on Canvas using WPF

In my WPF program I'm using canvas as a place to render drawings so when it is in line-drawing mode any unfinished line follows mouse cursor and waiting for another click to find the ending point of line. 在我的WPF程序中,我将画布用作渲染图形的位置,因此当它处于线条绘制模式时,任何未完成的线条都会跟随鼠标光标并等待再次单击以找到线条的终点。

One visual problem is that most of the time lines doesn't follow the cursor super fast... even sometimes I can see there is a delay. 一个视觉问题是,大多数时间线并没有很快跟随光标……甚至有时我可以看到有延迟。 It finds it anyway but it doesn't stick to moving cursor as it is done in other drawing applications eg autocad or soildworks ... 无论如何,它都能找到它,但它不会像在其他绘图应用程序(例如autocad或Soildworks)中所做的那样粘在移动光标上。

What am I missing here? 我在这里想念什么?

Thanks, 谢谢,

As @PeteBaughman said, it would help to know a bit more about your code, trying posting a sample of how you add and edit the line. 正如@PeteBaughman所说,尝试发布有关如何添加和编辑行的示例,将有助于您进一步了解代码。 One possible solution though, is, if you haven't already done so, trying capturing the mouse: 但是,一种可能的解决方案是,如果尚未这样做,请尝试捕获鼠标:

XAML: XAML:

<Canvas OnMouseDown="MyCanvas_MouseDown" ... />

Code-Behind: 代码隐藏:

public void MyCanvas_MouseDown(object sender, MouseButtonEventArgs e){
    MyCanvas.CaptureMouse();
}

This should ensure you're getting all the mouse events and was the solution to a very similar problem I recently had. 这应该确保您获得所有鼠标事件,并且是我最近遇到的一个非常类似的问题的解决方案。

Regards, 问候,

Richard 理查德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM