我正在为 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";
}