2013-04-16 84 views
18

我在處理畫布上的鼠標事件時遇到問題。我想用鼠標繪製它,並且我已經提出了這些事件處理程序,但是當我開始繪製時它們不會執行任何操作。WPF - 在畫布上繪製鼠標事件

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); 
     } 
    } 

你能告訴我什麼是缺失或如何重寫它,以便它可以開始工作,幫助我嗎?

回答

45

我敢打賭,你的畫布不接收鼠標事件,因爲它的背景屬性設置爲透明

這對我工作得很好。

enter image description here

<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); 
      } 
     } 

    } 
} 
+3

沒錯。這正是我所做的。謝謝。 –

+0

如何更新點擊捕獲來解決菜單造成的偏移? – Benjin

+8

而不是在GetPosition中傳遞對窗口的引用,而是傳遞對Canvas的引用,而不是將coords與其相關聯。 – Andy

0
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); 
    } 
} 
+1

這可以工作,但形狀將在** _Mouse_up_之後顯示** –

1

當使用線,粗線(line.StrokeThickness = 20)看起來是這樣的:

enter image description here

所以,我想折線和工作正常(。從這個例子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); 
    } 
}; 
1

簡單使用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>