相当于绘图应用程序的橡皮擦、c#、silverlight、wp7

发布于 2024-11-09 04:45:41 字数 577 浏览 0 评论 0原文

我正在开发一个简单的绘图应用程序,以进一步提高我的技能,但我似乎无法理解橡皮擦工具的逻辑。该应用程序仅使用 Line 类在用户移动手指时创建线条。对于橡皮擦工具,我尝试使用 VisualTreeHelper,如下所示:

List<UIElement> elements = (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(tree), ContentPanelCanvas);

            foreach (UIElement element in elements)
            {
                if (element is Line)
                {
                    this.ContentPanelCanvas.Children.Remove(element);
                }
            }

它在某些时候可能非常缓慢且滞后。有时我必须触摸该区域超过 5 次才能摆脱那里的线。

有替代方案吗?

I'm working on a simple drawing app to further advance my skills and I can't seem to get the logic down for an eraser tool. The app simply uses the Line class to create lines as the user moves their finger. For the eraser tool I tried using the VisualTreeHelper as follows:

List<UIElement> elements = (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(tree), ContentPanelCanvas);

            foreach (UIElement element in elements)
            {
                if (element is Line)
                {
                    this.ContentPanelCanvas.Children.Remove(element);
                }
            }

It at some points but can be very slow and laggy. Sometimes I would have to touch the area more than 5 times to get rid of the line there.

Is there an alternative to this?

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

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

发布评论

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

评论(3

莫相离 2024-11-16 04:45:41

e.GetPosition(tree) 将返回一个点。尝试使用以位置为中心的矩形

const double fingerMargin = 10.0;

Point p = e.GetPosition(tree);
Rect r = new Rect(p.X - fingerMargin, p.Y - fingerMargin, fingerMargin * 2, fingerMargin * 2); 

var elements = VisualTreeHelper.FindElementsInHostCoordinates(r, ContentPanelCanvas);

Line lineToRemove = elements.OfType<Line>().FirstOrDefault();
if (lineToRemove != null)
{
    ContentPanelCanvas.Children.Remove(lineToRemove);
}

请注意,不要将 FindElementsInHostCooperatives 的结果强制转换为 List,这是一个实现细节,文档仅保证它是一个 IEnumerable,除此之外这是不必要的演员阵容。

The e.GetPosition(tree) will be returning a point. Try instead using a Rect with the position as its center.

const double fingerMargin = 10.0;

Point p = e.GetPosition(tree);
Rect r = new Rect(p.X - fingerMargin, p.Y - fingerMargin, fingerMargin * 2, fingerMargin * 2); 

var elements = VisualTreeHelper.FindElementsInHostCoordinates(r, ContentPanelCanvas);

Line lineToRemove = elements.OfType<Line>().FirstOrDefault();
if (lineToRemove != null)
{
    ContentPanelCanvas.Children.Remove(lineToRemove);
}

Note don't cast the result of FindElementsInHostCoordinates to List<T>, that is an implementation detail, the documentation only guarantees it to be an IEnumerable<UIElement>, besides which it is an unnecessary cast.

拒绝两难 2024-11-16 04:45:41

您实际上正在寻找与单个像素的命中测试相匹配的元素集。如果你的线条很窄,那么它就像大海捞针;很难精确地击中线将其移除。

相反,您需要使用矩形而不是点来进行模糊匹配。您可以使用相同的 API,只是其矩形版本:

You are actually looking for the set of elements that match the hit test of a single pixel. If your lines are narrow, then it's like a needle in a haystack; it's very hard to hit the line precisely to remove it.

Instead you need to use a fuzzy match using a rectangle instead of a point. You can use the same API, just the rectangle version of it:

寂寞笑我太脆弱 2024-11-16 04:45:41

VisualTreeHelper.FindElementsInHostCoordinates(r, MainCanvas);
不返回任何元素。

VisualTreeHelper.FindElementsInHostCoordinates(r, MainCanvas);
Is not returning any Elements.

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