c# - 如何在 C# 中为 Windows Metro 风格应用程序在屏幕上绘图?

标签 c# windows coding-style draw microsoft-metro

我只是希望用户能够使用某种指针在屏幕上绘图。

我已经有了捕获指针位置的代码,但我不知道如何将像素或形状或其他任何内容放置到屏幕上。

我发现了这个有用的教程:
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;

最佳答案

缩写形式:

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

在 Metro/XAML 中无法覆盖 OnRender() 方法或类似方法。您的选择是将现有图形元素(例如来自 Shapes namespace )添加到 Canvas 或其他面板,或者直接操作位图中的像素并将该位图推送到图像元素中。

Metro/C# 仅具有保留模式图形绘制,这意味着它唯一会呈现的是已添加到 View 层次结构中的对象。您正在寻找的是某种即时模式图形绘制,例如

myCanvas.DrawLine( fromPoint, toPoint );

这可以在 JavaScript/HTML 项目中使用 HTML5's Canvas object 完成.遗憾的是,这就是我倾向于这样一个项目的方式。不幸的是,Microsoft 没有为 XAML 项目提供即时模式元素,但事实就是如此。 C++/DirectX 也是执行自定义绘图的选项,但需要对您在应用程序中执行的其他所有操作进行大量修改。

关于c# - 如何在 C# 中为 Windows Metro 风格应用程序在屏幕上绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8099466/

相关文章:

c# - 如何根据 if else 在 wpf mvvm 中使用 DataTriggers 更改按钮的样式

c# - Entity Framework 中的 SaveChanges 与 AcceptAllChanges

c# - 如何在边框后绑定(bind)我的文本?

在 64 位系统上未调用 Windows 错误检查回调

python - 处理错误和异常的Python方式

java - 编写简洁优雅的Java的技巧

c# - XDocument 未正确加载

Windows Kernel32.BatteryLifePercent = 255

windows - 是否可以使用 winmerge 从 cygwin 查看 git 差异?

.net - 推荐参数顺序