【问题标题】:WPF - Drawing on canvas with mouse eventsWPF - 使用鼠标事件在画布上绘图
【发布时间】:2013-04-16 12:59:44
【问题描述】:

我在处理画布上的鼠标事件时遇到问题。我想用鼠标在它上面画图,我想出了这些事件处理程序,但是当我开始画图时它们什么都不做。

    private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void paintSurface_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

您能帮我告诉我缺少什么或如何重写它以使其开始工作吗?

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    我敢打赌你的画布没有接收鼠标事件,因为它的背景属性设置为透明

    这对我来说很好。

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Canvas  Name="paintSurface" MouseDown="Canvas_MouseDown_1" MouseMove="Canvas_MouseMove_1" >
            <Canvas.Background>
                <SolidColorBrush Color="White" Opacity="0"/>
            </Canvas.Background>
        </Canvas>
    </Window>
    
    
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        public partial class MainWindow : Window
        {
    
            Point currentPoint = new Point();
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                if (e.ButtonState == MouseButtonState.Pressed)
                    currentPoint = e.GetPosition(this);
            }
    
            private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Line line = new Line();
    
                    line.Stroke = SystemColors.WindowFrameBrush;
                    line.X1 = currentPoint.X;
                    line.Y1 = currentPoint.Y;
                    line.X2 = e.GetPosition(this).X;
                    line.Y2 = e.GetPosition(this).Y;
    
                    currentPoint = e.GetPosition(this);
    
                    paintSurface.Children.Add(line);
                }
            }
    
        }
    }
    

    【讨论】:

    • 是的。这正是我所做的。谢谢。
    • 如何更新点击捕获以解决菜单造成的偏移?
    • 而不是在 GetPosition 中传递对窗口的引用,而是传递对 Canvas 的引用,因此坐标是相对于它的。
    • +1 对安迪的评论。如果您添加边距,您会看到差异
    • @Andy 我是否将 PaintSurface 传递给 all 的 GetPosition 函数(你有三个)?我仍然得到奇怪的结果:无论我点击哪里,它都以画布的边缘为起点并在点击的点上画一条线,然后正常继续面部
    【解决方案2】:

    简单使用 InkCanvas

     <InkCanvas x:Name="InkCanvas" x:FieldModifier="public" Background="Transparent" Opacity="1" EditingMode="GestureOnly" ForceCursor="True" Cursor="Pen" >
                                <InkCanvas.DefaultDrawingAttributes>
                                    <DrawingAttributes Color="White" Width="7" Height="7" />
                                </InkCanvas.DefaultDrawingAttributes>
                            </InkCanvas>
    

    【讨论】:

      【解决方案3】:

      使用 Line 时,粗线(line.StrokeThickness = 20)如下所示:

      所以我尝试了 PolyLine 并且工作正常。(来自此示例 http://www.c-sharpcorner.com/uploadfile/mahesh/polyline-in-wpf/

      Canvas.MouseMove += (sender, args) =>
      {
          if (args.LeftButton == MouseButtonState.Pressed)
          {
              Polyline polyLine;
              if (PathModeCanvas.Children.Count == 0)
              {
                  polyLine = new Polyline();
                  polyLine.Stroke = new SolidColorBrush(Colors.AliceBlue);
                  polyLine.StrokeThickness = 10;
      
                  Canvas.Children.Add(polyLine);
              }
      
              polyLine = (Polyline)Canvas.Children[0];
              Point currentPoint = args.GetPosition(Canvas);
              polyLine.Points.Add(currentPoint);
         }
      };
      

      【讨论】:

        【解决方案4】:
        public partial class MainWindow : Window
            {
                Line newLine;
                Point start;
                Point end;
        
            public MainWindow()
            {
                InitializeComponent();
            }
        
            private void DrawCanvas_MouseDown_1(object sender, MouseButtonEventArgs e)
            {
                start = e.GetPosition(this);
            }
        
            private void DrawCanvas_MouseMove_1(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    end = e.GetPosition(this);
                }
            }
        
            private void DrawCanvas_MouseUp_1(object sender, MouseButtonEventArgs e)
            {
        
                newLine = new Line();
                newLine.Stroke = SystemColors.WindowFrameBrush;
                newLine.X1 = start.X;
                newLine.Y1 = start.Y;
                newLine.X2 = end.X;
                newLine.Y2 = end.Y;
        
                DrawCanvas.Children.Add(newLine);
            }
        }
        

        【讨论】:

        • 这可行,但形状将在 Mouse_up 之后显示
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-18
        • 2016-06-07
        • 1970-01-01
        • 1970-01-01
        • 2019-12-11
        • 2013-01-13
        • 1970-01-01
        相关资源
        最近更新 更多