我只是希望用户能够使用某种指针在屏幕上绘图。
我已经有了捕获指针位置的代码,但我不知道如何将像素或形状或其他任何内容放置到屏幕上。
我发现了这个有用的教程:
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;
最佳答案
缩写形式:
- 将
Line
和Rectangle
添加到面板 - 直接操作位图
- 在 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/