如何在C#的Windows Metro Style应用程序中在屏幕上绘图?

7

我只是希望用户能够通过某种指针在屏幕上绘制。

我已经有了捕获指针位置的代码,但我无法想出如何将像素或形状放置到屏幕上。

我找到了这个有用的教程:
http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=137

我一直在查阅这里的文档:
http://msdn.microsoft.com/en-us/library/windows/apps/hh465055(v=VS.85).aspx

目前为止还没有什么进展。=( 教程是针对Windows Phone 7的,所以有些不同。=\ 请求帮助?=)

这是我目前为止的绘图部分:

    private void Image_PointerPressed(object sender, PointerEventArgs e)
    {
        Debug.WriteLine("Image_PointerPressed");
        isTracing = true;
    }

    private void Image_PointerReleased(object sender, PointerEventArgs e)
    {
        Debug.WriteLine("Image_PointerReleased");
        isTracing = false;
    }

    private void Image_PointerMoved(object sender, PointerEventArgs e)
    {
        Debug.WriteLine("Image_PointerMoved");
        Debug.WriteLine(e.GetCurrentPoint(this).Position);
        if (isTracing)
        {
            Debug.WriteLine("isTracing");

            Point pos = e.GetCurrentPoint(this).Position;
            Color color = Colors.Green;
            Line line = new Line() { X1 = pos.X, X2 = pos.X + 1, Y1 = pos.Y, Y2 = pos.Y + 1 };
            line.Stroke = new SolidColorBrush(color);
            line.StrokeThickness = 15;
            //// So how do I draw this line onto the screen?? ////

        }

    }

参考其他代码:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Multimedia.FFmpeg;
    using Windows.Foundation;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.Storage.Streams;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Shapes;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Input;
// 是否启用追踪 bool isTracing = false;

我也找到了这个教程,但还是没有帮助。http://www.windowsphonegeek.com/tips/drawing-in-wp7-1-getting-started-and-line-shape - Crystal
你现有的代码有什么问题? - Merlyn Morgan-Graham
我没有在屏幕上绘制任何东西。我不知道用什么来做这件事。(顺便说一句,谢谢你的评论!) - Crystal
5个回答

7

简述:

  • LineRectangle添加到面板中
  • 直接操作位图
  • 在JavaScript/HTML项目中使用HTML5 Canvas元素
  • 用C++/DirectX编写整个程序

Metro/XAML没有办法覆盖OnRender()方法或类似的方法。你可以选择将已有的图形元素(例如来自Shapes namespace)添加到Canvas或其他Panel中,或者直接操作位图像素并将其推入Image元素。

Metro/C#仅支持保留模式图形绘制,这意味着它只会渲染已添加到视图层次结构中的对象。你需要的是某种立即模式图形绘制,例如

myCanvas.DrawLine( fromPoint, toPoint );

这可以在JavaScript/HTML项目中使用HTML5的Canvas对象来完成。不幸的是,对于这样的项目,我倾向于使用这种方式。遗憾的是,微软没有为XAML项目提供即时模式元素,但事实就是如此。C++/DirectX也是进行自定义绘图的选项,但需要对应用程序中的所有其他内容进行大量重新工作。


2

0

这个示例项目包含用C#/XAML编写的Win 8 Store应用程序屏幕绘制代码:

http://code.msdn.microsoft.com/windowsapps/Drawing-on-a-Canvas-with-33510ae6

这是相关的 C# 文件:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using Windows.Devices.Input; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI; 
using Windows.UI.Input; 
using Windows.UI.Input.Inking; //we need to add this name space in order to have many functions  
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using Windows.UI.Xaml.Shapes; 
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 

namespace DrawingOnCanvasWithInkPen 
{ 
/// <summary> 
/// An empty page that can be used on its own or navigated to within a Frame. 
/// </summary> 
public sealed partial class MainPage : Page 
{ 
    InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager(); 
    private uint _penID; 
    private uint _touchID; 
    private Point _previousContactPt; 
    private Point currentContactPt; 
    private double x1; 
    private double y1; 
    private double x2; 
    private double y2; 


    public MainPage() 
    { 
        this.InitializeComponent(); 

        MyCanvas.PointerPressed += new PointerEventHandler(MyCanvas_PointerPressed); 
        MyCanvas.PointerMoved += new PointerEventHandler(MyCanvas_PointerMoved); 
        MyCanvas.PointerReleased += new PointerEventHandler(MyCanvas_PointerReleased); 
        MyCanvas.PointerExited += new PointerEventHandler(MyCanvas_PointerReleased); 
    } 


    #region PointerEvents 
    private void MyCanvas_PointerReleased(object sender, PointerRoutedEventArgs e) 
    { 
        if (e.Pointer.PointerId == _penID) 
        { 
            Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(MyCanvas); 

            // Pass the pointer information to the InkManager.  
            _inkKhaled.ProcessPointerUp(pt); 
        } 

        else if (e.Pointer.PointerId == _touchID) 
        { 
            // Process touch input 
        } 

        _touchID = 0; 
        _penID = 0; 

        // Call an application-defined function to render the ink strokes. 


        e.Handled = true; 
    } 

    private void MyCanvas_PointerMoved(object sender, PointerRoutedEventArgs e) 
    { 
        if (e.Pointer.PointerId == _penID) 
        { 
            PointerPoint pt = e.GetCurrentPoint(MyCanvas); 

            // Render a red line on the canvas as the pointer moves.  
            // Distance() is an application-defined function that tests 
            // whether the pointer has moved far enough to justify  
            // drawing a new line. 
            currentContactPt = pt.Position; 
            x1 = _previousContactPt.X; 
            y1 = _previousContactPt.Y; 
            x2 = currentContactPt.X; 
            y2 = currentContactPt.Y; 

            if (Distance(x1, y1, x2, y2) > 2.0) // We need to developp this method now  
            { 
                Line line = new Line() 
                { 
                    X1 = x1, 
                    Y1 = y1, 
                    X2 = x2, 
                    Y2 = y2, 
                    StrokeThickness = 4.0, 
                    Stroke = new SolidColorBrush(Colors.Green) 
                }; 

                _previousContactPt = currentContactPt; 

                // Draw the line on the canvas by adding the Line object as 
                // a child of the Canvas object. 
                MyCanvas.Children.Add(line); 

                // Pass the pointer information to the InkManager. 
                _inkKhaled.ProcessPointerUpdate(pt); 
            } 
        } 

        else if (e.Pointer.PointerId == _touchID) 
        { 
            // Process touch input 
        } 

    } 

    private double Distance(double x1, double y1, double x2, double y2) 
    { 
        double d = 0; 
        d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2)); 
        return d; 
    } 

    private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e) 
    { 
        // Get information about the pointer location. 
        PointerPoint pt = e.GetCurrentPoint(MyCanvas); 
        _previousContactPt = pt.Position; 

        // Accept input only from a pen or mouse with the left button pressed.  
        PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType; 
        if (pointerDevType == PointerDeviceType.Pen || 
                pointerDevType == PointerDeviceType.Mouse && 
                pt.Properties.IsLeftButtonPressed) 
        { 
            // Pass the pointer information to the InkManager. 
            _inkKhaled.ProcessPointerDown(pt); 
            _penID = pt.PointerId; 

            e.Handled = true; 
        } 

        else if (pointerDevType == PointerDeviceType.Touch) 
        { 
            // Process touch input 
        } 
    } 

    #endregion 

    /// <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) 
    { 
    } 
} 

}

和 xaml 文件:

<Page 

x:Class="DrawingOnCanvasWithInkPen.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:DrawingOnCanvasWithInkPen" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <Canvas Name="MyCanvas" Background="White" HorizontalAlignment="Left" Height="513" Margin="83,102,0,0" VerticalAlignment="Top" Width="1056"/> 

</Grid> 

在当前状态下,它只处理笔或鼠标输入 - 但我也进行了轻微修改,使其适用于触摸输入。

0
你代码的主要问题在于你没有将该行代码附加到任何XAML元素上,我建议你将其附加到Canvas元素上,就像这样:
newCanvas.Children.Add(line);

另一种选择是使用现代组件绘图库,它适用于WinRT,使用类似于.NET Graphics的调用,并直接在XAML画布上绘制。请注意,如果您想将图像保存为位图,您可能还需要使用WritableBitmapEx,因为XAML画布无法渲染为位图。


0
你应该将这行代码添加到 UI 元素,比如画布(Canvas)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接