2012-06-26 73 views
2

我正在開發像Windows 8的應用程序的油漆。我已經嘗試過,但我甚至不能開發一個線條繪畫工具。我的應用程序將有免費的手工工具,線條,矩形,橢圓,圓形繪圖工具,橡皮擦,將畫布內容保存爲JPG。我正在使用畫布工具進行繪製。我對各種「指針」事件感到困惑。我已經檢查了一些WPF應用程序樣本,但我無法完全移植這些應用程序。地鐵風格應用程序中的全面塗料(繪圖)應用程序

所以,請指導我一些編碼,請給我工作代碼。

在這裏,我附上線繪圖的示例代碼。但是在該行中不斷繪製,因爲沒有規定檢查是否按下鼠標左鍵。

<!-- XAML CODE --> 
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
     <StackPanel Orientation="Horizontal" Margin="0,-700,0,0" Grid.Row="0"> 
      <Button x:Name="btnLine" Click="btnLine_Click" Height="50" Width="auto" Content="Line" Grid.Row="0"/> 
      <Button x:Name="btnElipse" Click="btnElipse_Click" Height="50" Width="auto" Content="Elipse" Grid.Row="0"/> 
      <Button x:Name="btnPencil" Click="btnPencil_Click" Height="50" Width="auto" Content="Pencil" Grid.Row="0"/> 
     </StackPanel> 
    <Canvas Name="canvas" Background="AntiqueWhite" Margin="0,65,0,0"/> 

/* C# Code*/ 

void canvas_PointerEntered(object sender, PointerRoutedEventArgs e) 
    { 
     switch (DrawingTool) 
     { 
      case "Line": 
       { 
        clickPoint = e.GetCurrentPoint(canvas).Position; 
        newLine = new Line(); 
        newLine.Fill = new SolidColorBrush(Windows.UI.Colors.Black); 
        newLine.StrokeLineJoin = PenLineJoin.Bevel; 
        newLine.X1 = clickPoint.X; 
        newLine.Y1 = clickPoint.Y; 
        newLine.X2 = clickPoint.X + 10; 
        newLine.Y2 = clickPoint.Y + 10; 
        newLine.StrokeThickness = 2; 
        canvas.Children.Add(newLine); 
        int zindex = canvas.Children.Count; 
        Canvas.SetZIndex(newLine, zindex); 
       } 
       break; 

      case "Pencil": 
       { 
        startPoint = e.GetCurrentPoint(canvas).Position; 
        line = new Polyline(); 
        line.Stroke = new SolidColorBrush(Windows.UI.Colors.Black); 
        line.StrokeThickness = 2.0; 
        canvas.Children.Add(line); 
       } 
       break; 

      case "Ellipse": 
       { 
        newEllipse = new Ellipse(); 
        newEllipse.StrokeThickness = 2; 
        newEllipse.Stroke = new SolidColorBrush(Windows.UI.Colors.Black); 
        newEllipse.StrokeLineJoin = PenLineJoin.Bevel; 
        newEllipse.Width = clickPoint.X; 
        newEllipse.Height = clickPoint.Y; 
        canvas.Children.Add(newEllipse); 
       } 
       break; 

      default: 
       break; 
     } 
    } 

    void canvas_PointerMoved(object sender, PointerRoutedEventArgs e) 
    { 
     switch (DrawingTool) 
     { 
      case "Pencil": 
       { 
        if (true) 
        { 
         Point currentPoint = e.GetCurrentPoint(canvas).Position; 
         if (startPoint != currentPoint) 
         { 
          line.Points.Add(currentPoint); 
         } 
        } 
       } 
       break; 

      case "Line": 
       { 
        drawPoint = e.GetCurrentPoint(canvas).Position; ; 
        if (newLine != null) 
        { 
         newLine.X2 = drawPoint.X; 
         newLine.Y2 = drawPoint.Y; 
        } 
       } 
       break; 

      default: 
       break; 
     } 
    } 

    void canvas_PointerReleased(object sender, PointerRoutedEventArgs e) 
    { 
     newLine = null; 
    } 

    string DrawingTool; 
    Line newLine; 
    Ellipse newEllipse; 
    Point clickPoint; 
    Point drawPoint; 
    Point startPoint; 
    Polyline line; 

    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. The Parameter 
    /// property is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
    } 

    private void btnPencil_Click(object sender, RoutedEventArgs e) 
    { 
     DrawingTool = "Pencil"; 
    } 

    private void btnLine_Click(object sender, RoutedEventArgs e) 
    { 
     DrawingTool = "Line"; 
    } 

    private void btnElipse_Click(object sender, RoutedEventArgs e) 
    { 
     DrawingTool = "Ellipse"; 
    } 

回答

1

在畫布訂閱鼠標按下和MouseUp事件和beteween收到鼠標按下並收到鼠標鬆開鼠標位置繪製一個像素。

+0

我正在開發的Windows 8 Metro風格的應用程序,它沒有鼠標鬆開上下活動 – Xyroid

+0

它必須有一些事件發生的事件點擊。搜索正確的名稱。 –

+0

我已經解決了我的問題。感謝您的回覆,但現在我面臨一些其他問題,下面給出 http://stackoverflow.com/questions/11239980/saving-canvas-as-image-in-metro-style-app http:///stackoverflow.com/questions/11227031/erasing-the-content-from-canvas-where-mouse-is-being-dragged – Xyroid

3

我現在成功地開發了基本的metro風格的塗料應用程序。歡迎提出建議。你可以下載源代碼的形式codeproject

1
canvas.PointerMoved += _canvas_PointerMoved; 
canvas.PointerPressed += _canvas_PointerPressed; 
canvas.PointerReleased += _canvas_PointerReleased; 

你需要處理指針輸入

相關問題