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";
    }
4

3 に答える 3

3

現在、基本的なメトロ スタイル ペイント アプリケーションの開発に成功しています。提案を歓迎します。ソース形式のコードプロジェクトをダウンロードできます

于 2012-07-20T05:35:45.583 に答える
1
canvas.PointerMoved += _canvas_PointerMoved;
canvas.PointerPressed += _canvas_PointerPressed;
canvas.PointerReleased += _canvas_PointerReleased;

ポインタ入力を処理するために必要なイベントですか

于 2012-11-27T15:43:33.380 に答える
1

キャンバスで、mousedownおよびmouseupイベントをサブスクライブし、mousedownとmouseupの間にあるマウス位置にピクセルを描画します。

于 2012-06-26T13:41:50.797 に答える