65.9K
CodeProject 正在变化。 阅读更多。
Home

Metro Paint

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (26投票s)

2012年7月11日

CPOL

3分钟阅读

viewsIcon

130153

downloadIcon

7280

一个用于基本绘图功能的Metro风格应用程序。

引言

这个Metro风格的应用程序用于绘制基本的形状,如线条、矩形、圆形、椭圆形、徒手形状、橡皮擦和清除屏幕。它还提供选择笔画粗细、边框颜色、填充颜色和手写识别的功能。它允许将识别的文本保存为文本文件,并且只有墨迹笔画可以保存为图像。这还是初始版本,因此存在一些限制,例如将整个绘图保存为图像、调整已绘制组件的大小和移动等。

Using the Code

基本上,这个应用程序使用`Windows.UI.Xaml.Shapes`命名空间和指针事件,因为没有鼠标事件。这里所有的绘图功能都是通过`PointerMoved`、`PointerReleased`和`PointerPressed`事件完成的。所有的绘图都是在画布上完成的。

为了选择绘图工具,我使用了`switch case`。这些工具基本上是按钮,使用的图标只是“Segoe UI Symbol”字体。我使用字符映射来获取所需的图标。

为了在`PointerPressed`事件中绘制组件,当前坐标被保存,然后在`PointerMoved`事件之后,第二个坐标被保存。这些坐标可以直接用于线条绘制,但对于其他形状,我通过坐标的差值来获取高度和宽度。徒手绘图工具只是在`PointerPressed`事件期间添加路径中的点。Metro应用程序无法以编程方式关闭,因此关闭按钮将暂停应用程序,一段时间后任务管理器将杀死该应用程序以释放资源。

颜色组合框是通过编程方式填充的。代码片段如下:

var colors = typeof(Colors).GetTypeInfo().DeclaredProperties;
foreach (var item in colors)
{
    cbBorderColor.Items.Add(item);
    cbFillColor.Items.Add(item);
}

由于在WinRT中没有渲染方法,因此无法将绘制的对象保存为图像。此外,`AdornerDecorator`也不可用,因此无法调整组件的大小和移动。我希望微软能用缺失的功能更新WinRT。

对于手写识别,我使用了`Windows.UI.Input.Inking`命名空间。基本概念是使用`InkManager`类。它提供了管理一个或多个`InkStroke`对象的输入、操作和处理(包括手写识别)的属性和方法。当用户在画布上写东西时,在该路径中覆盖的所有点都保存为`InkManager`对象。`InkManager`类提供了一个名为`RecognizeAsync()`的异步方法。识别的结果是`InkRecognitionResult`对象的集合。为了获取文本组件,我使用了`InkRecognitionResult`类的`GetTextCandidates()`方法,它检索被识别为手写识别潜在匹配项的字符串集合。代码片段如下:

private async void btnRecognize_Click(object sender, RoutedEventArgs e)
{
    try
    {
        txtRecognizedText.Visibility = Windows.UI.Xaml.Visibility.Visible;
        btnSaveRecognizedText.Visibility = Windows.UI.Xaml.Visibility.Visible;
        canvas.SetValue(Grid.RowProperty, 3);
        canvas.SetValue(Grid.RowSpanProperty, 1);
        MyInkManager.Mode = InkManipulationMode.Inking;
        x = await MyInkManager.RecognizeAsync(InkRecognitionTarget.Recent);
        MyInkManager.UpdateRecognitionResults(x);
        foreach (InkRecognitionResult i in x)
        {
            RecognizedText = i.GetTextCandidates();
            FinalRecognizedText += " " + RecognizedText[0];
            txtRecognizedText.Text += FinalRecognizedText;
        }
        FinalRecognizedText = string.Empty;

    }
    catch (Exception)
    {
        if (canvas.Children.Count == 0)
        {
            var MsgDlg = new MessageDialog("Your screen has no handwriting. " + 
              "Please write something with pencil tool then click recognize.", 
              "Error while recognizing");
            MsgDlg.ShowAsync();
        }
        else
        {
            var MsgDlg = new MessageDialog("Please clear the screen then write " + 
              "something with pencil tool", "Error while recognizing");
            MsgDlg.ShowAsync();
        }
    }
}

关注点

这个应用程序演示了用户如何绘制形状以及如何在XAML/C# Metro风格应用程序中执行手写识别。我请求其他开发人员用他们的评论和建议来增强我的应用程序。我非常感谢CodeProjectStackOverflowMSDN论坛帮助我解决了问题。

从这里复制粘贴应用程序到Windows商店

有一天,我正在寻找一个更好的绘画应用程序,我发现了这两个应用程序,它们完全是从这篇文章中复制粘贴的。我建议微软不要接受这种垃圾应用程序。

© . All rights reserved.