c# - Metro 风格应用程序中成熟的绘画(绘图)应用程序

标签 c# xaml drawing windows-8 microsoft-metro

我正在为 Windows 8 开发类似绘画的应用程序。我已经尝试过,但我什至无法开发线条绘制工具。我的应用程序将有徒手工具,直线,矩形,椭圆,圆形绘图工具,橡皮擦,将 Canvas 内容保存为JPG。我正在使用 Canvas 工具进行绘图。我对各种“指针”事件感到困惑。我已经在 WPF 中检查了一些类似 Paint 的应用程序示例,但我无法完全移植这些应用程序。

所以请指导我一些编码,请提供我的工作代码。

这里我附上画线的示例代码。但该线不断绘制,因为没有规定检查鼠标左键是否按下。

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

最佳答案

我现在成功开发了基本的 Metro 风格油漆应用程序。欢迎提出建议。您可以下载源表格codeproject

关于c# - Metro 风格应用程序中成熟的绘画(绘图)应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208267/

相关文章:

css - 如何增加文本框控件 XAML 的大小

c++ - 关于二维绘图和绘画的任何好的教程?

android - 如何在 Android 中对旋转图像进行 HitTest ?

c# - 第一次点击按钮很慢

c# - 为什么此事件日志的 EntryWritten 事件永远不会触发?

c# - 无法理解 "A field initializer cannot reference the nonstatic field"错误?

c# - 如何在 C# 中使 UserControls BackColor 透明?

c# - 正则表达式性能优化

c# - 单击按钮MVVM时获取commandparameter值

c# - 克隆具有相似模式的样式的友好方式?