我正在开发像Windows 8的应用程序的油漆。我已经尝试过,但我甚至不能开发一个线条绘画工具。我的应用程序将有免费的手工工具,线条,矩形,椭圆,圆形绘图工具,橡皮擦,将画布内容保存为JPG。我正在使用画布工具进行绘制。我对各种“指针”事件感到困惑。我已经检查了一些WPF应用程序样本,但我无法完全移植这些应用程序。地铁风格应用程序中的全面涂料(绘图)应用程序
所以,请指导我一些编码,请给我工作代码。
在这里,我附上线绘图的示例代码。但是在该行中不断绘制,因为没有规定检查是否按下鼠标左键。
<!-- 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";
}
我正在开发的Windows 8 Metro风格的应用程序,它没有鼠标松开上下活动 – Xyroid
它必须有一些事件发生的事件点击。搜索正确的名称。 –
我已经解决了我的问题。感谢您的回复,但现在我面临一些其他问题,下面给出 http://stackoverflow.com/questions/11239980/saving-canvas-as-image-in-metro-style-app http:///stackoverflow.com/questions/11227031/erasing-the-content-from-canvas-where-mouse-is-being-dragged – Xyroid