如何更改 InkCanvas 的绘制方式?

发布于 2024-08-21 15:01:25 字数 1716 浏览 5 评论 0原文

我已经为此搜索了示例,但我遇到的示例似乎专注于更简单的内容,例如设置 InkCanvas DefaultDrawingAttributes(例如宽度、高度、颜色等)。似乎没有很多相关材料。

例如,如果我按住鼠标按钮,我可以看到它绘制线条。如果我想绘制椭圆而不是直线,或者在直线起点和终点之间的采样点周围绘制椭圆,该怎么办?

我知道我可以通过 StrokeCollected 活动获得新积分,但除此之外我不知道该去哪里。 这家伙看起来好像他让msdn的代码工作了,但我做不到。我只知道如何使用XAML构建界面,而且似乎也没有示例。

编辑

创建了一个名为 thisIsNotNice 的 StrokeCollection 类变量,在构造函数中初始化并执行了以下操作:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
    myInkCanvas.Strokes = thisIsNotNice;

    foreach (StylusPoint p in e.Stroke.StylusPoints)
    {
        StylusPointCollection spc = new StylusPointCollection();
        spc.Add(p);
        Stroke s = new Stroke(spc);
        s.DrawingAttributes.Height = 3;
        s.DrawingAttributes.Width = 3;
        thisIsNotNice.Add(s);
     }
     e.Handled = true;
}

但它没有按预期工作。椭圆被绘制了,但是鼠标绘制的线条仍然存在。另外,由于某种原因,第一次它按预期工作,只绘制椭圆,但后来它同时绘制椭圆和线条。但是,如果我这样做:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
     myInkCanvas.Strokes = new System.Windows.Ink.StrokeCollection();
     e.Handled = true;
}

这些线条不会保留在屏幕上。所以,我不明白为什么它们在上面的代码中没有被删除。

如果我这样做:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
    foreach (Stroke s in myInkCanvas.Strokes)
            System.Diagnostics.Trace.WriteLine(s);
    e.Handled = true;
}

我还可以看到画布包含线条笔划。

虽然在将笔画添加到集合中后擦除笔画远非理想,但它至少达到了我想要的效果。我可以将线条颜色设置为与背景相同,但这样我将无法仅检索省略号。我也可以将它们复制到一个单独的集合中,但这太糟糕了。

I've searched for examples for this, but the ones I've ran across seem to focus on simpler stuff like setting the InkCanvas DefaultDrawingAttributes such as Width, Height, Color etc. Doesn't seem like there's a lot of material for this.

For example, if I hold down the mouse button I can see it drawing lines. What if I want to draw ellipses instead of lines, or draw ellipses around sampled points between the start and end of the line?

I know I can get new points with the StrokeCollected event, but beyond that I have no idea where to go. This guy seemed like he got msdn's code working, but I couldn't do it. I only know how to build the interface using XAML, and there doesn't seem to be a sample either.

edit

Created a StrokeCollection class variable called thisIsNotNice, initialized in the constructor and did this:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
    myInkCanvas.Strokes = thisIsNotNice;

    foreach (StylusPoint p in e.Stroke.StylusPoints)
    {
        StylusPointCollection spc = new StylusPointCollection();
        spc.Add(p);
        Stroke s = new Stroke(spc);
        s.DrawingAttributes.Height = 3;
        s.DrawingAttributes.Width = 3;
        thisIsNotNice.Add(s);
     }
     e.Handled = true;
}

But it doesn't work as it should. The ellipses are drawn, but the lines drawn by the mouse are still there. Also, for some reason, the first time it works as it should, drawing just the ellipses, but afterward it draws both the ellipses and the lines. But, if I do this instead:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
     myInkCanvas.Strokes = new System.Windows.Ink.StrokeCollection();
     e.Handled = true;
}

The lines aren't kept on the screen. So, I don't understand why they aren't being erased in the above code.

If I do this:

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
    foreach (Stroke s in myInkCanvas.Strokes)
            System.Diagnostics.Trace.WriteLine(s);
    e.Handled = true;
}

I can also see that the canvas contains the line strokes.

While erasing the strokes after they have been added to the collection is far from ideal, it at least does what I want. I could set up the line color to be the same of the background, but then I wouldn't be able to retrieve just the ellipses. I could copy them to a separate collection too, but that's just awful.

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

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

发布评论

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

评论(1

他不在意 2024-08-28 15:01:25

听起来您想自定义笔画在 inkCanvas 上的显示方式。这里有两个单独的事情需要考虑:

1)在抬起笔之前墨水从笔上流出时的外观(DynamicRenderer 负责此操作,它在另一个线程上运行以确保墨水始终快速)。听起来你已经对这个问题的解决方案感到满意了

2) 最终笔触在画布上的外观。要自定义它,您可以尝试对 Stroke 进行子类化,覆盖:
protected override void DrawCore(DrawingContext DrawingContext, DrawingAttributes DrawingAttributes);

每次你得到一个StrokeCollected(这也是你担心的同样可怕的事情,但你就这样了),你从画布中删除传入的笔划,并将其替换为你的自定义实现,从传入的笔划中窃取笔划数据。

您的 DrawCore 实现看起来像(伪代码):

foreach(sp in this.StylusPoints)
  drawingContext.DrawEllipse(RADIUS, sp.X, sp.Y)

为了不得到通常发生的行,您在任何时候都不会调用 base.DrawCore(context,attributes) 。

It sounds like you want to customize the way strokes appear on your inkCanvas. There are two separate things to consider here:

1) The way they look as the ink flows off the pen, before it is lifted (the DynamicRenderer, who runs on another thread to ensure that ink is always fast, is responsible for this. It sounds like you're happy with your solution to this already.

2) The way the eventual stroke sitting on the canvas looks. To customize this you might try subclassing Stroke, overriding:
protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes);

Each time you get a strokeCollected (and here's the same horrible thing you were worried about but there you go), you remove the incoming stroke from the canvas and replace it with your custom implementation, stealing the stroke data from the incoming one.

Your implementation of DrawCore would look something like (pseudocode):

foreach(sp in this.StylusPoints)
  drawingContext.DrawEllipse(RADIUS, sp.X, sp.Y)

And so as not to get the lines that normally happen you would not call base.DrawCore(context,attributes) at any point.

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