WP8.1触摸画图形

前端代码如下:

<Grid x:Name="GDContent">
<TextBlock x:Name="txtbTitle" Text="画图形" FontSize="60" Margin="24,50,0,0"></TextBlock>

</Grid>
<Page.BottomAppBar>
<CommandBar IsOpen="False">
<CommandBar.SecondaryCommands>
<AppBarButton Icon="Copy" Label="画圆形" Click="AppBarButton_Click"></AppBarButton>
<AppBarButton Icon="Cut" Label="画正方形" Click="AppBarButton_Click"></AppBarButton>
<AppBarButton Icon="Paste" Label="画直线" Click="AppBarButton_Click"></AppBarButton>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>

后台代码如下:

//随机数产生类
Random rd = new Random();
//判断是画图还是拖动
bool isDraw, isDrag;
Path path;

string DoShape = "画圆形";
EllipseGeometry EG;
RectangleGeometry RG;
LineGeometry LG;
string DragShape = "";
public Test()
{
this.InitializeComponent();
base.ManipulationMode = ManipulationModes.All;

}

/// <summary>
/// 在此页将要在 Frame 中显示时进行调用。
/// </summary>
/// <param name="e">描述如何访问此页的事件数据。
/// 此参数通常用于配置页。</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
AppBarButton ABB = sender as AppBarButton;
DoShape = ABB.Label;
txtbTitle.Text = ABB.Label;
}

protected override void OnManipulationStarted(ManipulationStartedRoutedEventArgs e)
{
//判断是否触摸在已画图形上,如果是就拖动,不是就画图形
if (e.OriginalSource is Path)
{
path = e.OriginalSource as Path;
if (path.Data is EllipseGeometry)
{
EG = path.Data as EllipseGeometry;
DragShape = "圆形";
}
else if (path.Data is RectangleGeometry)
{
RG = path.Data as RectangleGeometry;
DragShape = "正方形";
}
else if (path.Data is LineGeometry)
{
LG = path.Data as LineGeometry;
DragShape = "直线";
}
isDrag = true;
}
else
{
switch (DoShape)
{
case "画圆形":
EG = new EllipseGeometry();
//设置椭圆中心点
EG.Center = e.Position;
path = new Path();
//设置图形的边框颜色以及样式即实线虚线等
path.Stroke = new SolidColorBrush(Colors.Gold);
path.Data = EG;
break;
case "画正方形":
RG = new RectangleGeometry();
Rect re = new Rect(e.Position, e.Position);
RG.Rect = re;
path = new Path();
path.Stroke = new SolidColorBrush(Colors.Gold);
path.Data = RG;
break;
case "画直线":
Color cr = Color.FromArgb(255, (byte)rd.Next(256), (byte)rd.Next(256), (byte)rd.Next(256));
LG = new LineGeometry();
LG.StartPoint = e.Position;
LG.EndPoint = e.Position;
path = new Path();
path.Stroke = new SolidColorBrush(cr);
path.Data = LG;
break;
}
path.ManipulationMode = ManipulationModes.All;
GDContent.Children.Add(path);
isDraw = true;
}
e.Handled = true;
}

/// <summary>
/// 该事件可以在触摸过程中多次触发
/// </summary>
/// <param name="e"></param>
protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
{
Point p;
if (isDraw)
{
//从OnManipulationStarted开始到现在触摸改变的累加x-y轴
p = e.Cumulative.Translation;
double r = CountLength(p);
switch (DoShape)
{
case "画圆形":
//设置椭圆的x轴半径
EG.RadiusX = r;
//设置椭圆的y轴半径
EG.RadiusY = r;
break;
case "画正方形":
Rect re = RG.Rect;
Rect re1 = new Rect(re.X, re.Y, r, r);
RG.Rect = re1;
break;
case "画直线":
Point end = LG.StartPoint;
end.X += p.X;
end.Y += p.Y;
LG.EndPoint = end;
break;

}

}
else if (isDrag)
{
//OnManipulationDelta触发与上次触发改变的x-y轴的值
p = e.Delta.Translation;
switch (DragShape)
{
case "圆形":
Point ellip = EG.Center;
ellip.X += p.X;
ellip.Y += p.Y;
EG.Center = ellip;
break;
case "正方形":
Rect re = RG.Rect;
re.X += p.X;
re.Y += p.Y;
RG.Rect = re;
break;
case "直线":
Point startline = LG.StartPoint;
Point endline = LG.EndPoint;
startline.X += p.X;
startline.Y += p.Y;
endline.X += p.X;
endline.Y += p.Y;
LG.StartPoint = startline;
LG.EndPoint = endline;
break;
}
}
e.Handled = true;
}

/// <summary>
/// 通过改变的坐标根据勾股定理求出起始点到终点的长度
/// </summary>
/// <param name="end"></param>
/// <returns></returns>
private double CountLength(Point end)
{
return Math.Sqrt(end.X*end.X+end.Y*end.Y);

}

/// <summary>
/// 当触摸结束即手指离开界面时调用的方法
/// </summary>
/// <param name="e"></param>
protected override void OnManipulationCompleted(ManipulationCompletedRoutedEventArgs e)
{
if (isDrag)
{
isDrag = false;
}
else if(isDraw)
{
//声明一随机颜色
Color cr = Color.FromArgb(255, (byte)rd.Next(256), (byte)rd.Next(256), (byte)rd.Next(256));
//填充该颜色到图形中
path.Fill = new SolidColorBrush(cr);
isDraw = false;
}
e.Handled = true;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值