【问题标题】:How to submit events to InkCanvas in WPF manually?如何在 WPF 中手动向 InkCanvas 提交事件?
【发布时间】:2011-09-11 12:18:53
【问题描述】:

我如何能够手动提交事件以供 InkCanvas 接收?

我需要做的,是将 InkCanvas 的模式设置为墨迹模式,然后,将虚拟事件发送到 InkCanvas,这样我就可以获得用户使用真实鼠标的绘图行为。

谢谢

【问题讨论】:

    标签: wpf inkcanvas


    【解决方案1】:

    以下代码 sn-p 显示了在 InkCanvas 中绘制形状的示例:

    StylusPointCollection stroke1Points = new StylusPointCollection();
    stroke1Points.Add(new StylusPoint(50,10));
    stroke1Points.Add(new StylusPoint(90,50));
    stroke1Points.Add(new StylusPoint(10,50));
    stroke1Points.Add(new StylusPoint(50,10));
    
    Stroke stroke1 = new Stroke(stroke1Points);
    
    canvas.Strokes.Add(stroke1);            
    

    其中 canvasInkCanvas 类型。以上在画布中生成了一个三角形。


    “是的,如果对你有帮助,你可以接受这个答案。”

    【讨论】:

    • 这不是我想要的,我想要使用InkCanvas中内置的手绘,在Ink模式下激活,我想在没有真正的鼠标事件的情况下实现它,这可能吗?
    • 如果不是鼠标,输入是从哪里来的???无论形状如何,您都必须了解要点。
    • 我们有一个双笔板,通过串口连接,每支笔都流坐标,我需要将这些坐标传递给 InkCanvas,使用内置的手绘。
    • 这正是你可以用上面的代码做的。您可以将坐标作为触控笔点添加到 stroke1 集合。如果您检测到正在创建新笔划,则可以如上所述添加新的 StylusPointCollection。
    • 感谢 loxxy,但 InkCanvas 中内置的手绘比使用 StylusPointCollection 快得多。我已经尝试过 StylusPointCollection 方法,效果很好,但不如 InkCanvas 上的手绘速度快
    【解决方案2】:

    这样的?

        private void inkSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            inkSurface.CaptureMouse();
    
            _inkStroke = new Stroke(
                e.StylusDevice.GetStylusPoints(inkSurface));
            _inkStroke.DrawingAttributes.Width = 5;
            _inkStroke.DrawingAttributes.Height = 5;
            _inkStroke.DrawingAttributes.Color = Colors.Black;
    
            inkSurface.Strokes.Add(_inkStroke);
            e.Handled = true;
        }
    
        private void inkSurface_MouseMove(object sender, MouseEventArgs e)
        {
            if (_inkStroke != null)
            {
                _inkStroke.StylusPoints.Add(
                    e.StylusDevice.GetStylusPoints(inkSurface));
            }
            e.Handled = true;
        }
    
        private void inkSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            inkSurface.ReleaseMouseCapture();
            e.Handled = true;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      相关资源
      最近更新 更多