C# 创建可设置动画的2D wpf自定义形状
目前我正在从事一个项目,该项目涉及在WPF中设置自定义二维形状的动画。形状看起来像一个盒子,目前我似乎有一些小故障 下面是我的代码目前的样子C# 创建可设置动画的2D wpf自定义形状,c#,wpf,animation,2d,C#,Wpf,Animation,2d,目前我正在从事一个项目,该项目涉及在WPF中设置自定义二维形状的动画。形状看起来像一个盒子,目前我似乎有一些小故障 下面是我的代码目前的样子 namespace Wpftrial4 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { So
namespace Wpftrial4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SolidColorBrush fillcolor = new SolidColorBrush();
SolidColorBrush bordercolor = new SolidColorBrush();
public MainWindow()
{
InitializeComponent();
fillcolor.Color = Colors.WhiteSmoke;
bordercolor.Color = Colors.Blue;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Manually creating the required 2D Box shape
Path path_rectangle = new Path();
path_rectangle.Fill = fillcolor;
path_rectangle.Stroke = bordercolor;
path_rectangle.StrokeThickness = 5;
RectangleGeometry rg = new RectangleGeometry();
double width_rec = 100;
double height_rec = 50;
double left_rec = 130;
double top_rec = 100;
rg.Rect = new Rect(left_rec, top_rec, width_rec, height_rec);
path_rectangle.Data = rg;
canvas1.Children.Add(path_rectangle);
Path path2_top = new Path();
path2_top.Stroke = new SolidColorBrush(Colors.Brown);
path2_top.StrokeThickness = 6;
RectangleGeometry rg2 = new RectangleGeometry();
double width2 = 100;
double height2 = 2;
double left2 = 130;
double top2 = 90;
rg2.Rect = new Rect(left2, top2, width2, height2);
path2_top.Data = rg2;
canvas1.Children.Add(path2_top);
Path path3_topcover = new Path();
path3_topcover.Stroke = new SolidColorBrush(Colors.White);
path3_topcover.StrokeThickness = 5;
RectangleGeometry rg3 = new RectangleGeometry();
double width3 = 100;
double height3 = 5;
double left3 = 130;
double top3 = 100;
rg3.Rect = new Rect(left3, top3, width3, height3);
path3_topcover.Data = rg3;
canvas1.Children.Add(path3_topcover);
//combining part_Group type
GeometryGroup myGeometryGroup = new GeometryGroup();
myGeometryGroup.Children.Add(rg);
myGeometryGroup.Children.Add(rg3);
Path myPath = new Path();
myPath.Stroke = Brushes.Blue;
myPath.Data = myGeometryGroup;
canvas1.Children.Add(myPath);
////test animate
//myCustomAnimatedBox MB2 = new myCustomAnimatedBox(200, 200, 300, 250) { Stroke = Brushes.Black, StrokeThickness = 2 };
//canvas1.Children.Add(MB2); //new myCustomAnimatedBox(200, 200, 300, 250,500,500){Stroke = Brushes.Black, StrokeThickness = 2});
//PointAnimation pa2 = new PointAnimation();
////DoubleAnimation da = new DoubleAnimation();
//pa2.To = new Point(100, 100);
////da.By = 50;
//pa2.Duration = TimeSpan.FromSeconds(10);
////MB2.BeginAnimation(myCustomAnimatedBox.TopProperty,pa2);//(//MB2.BeginAnimation(myCustomAnimatedBox.CenterProperty, pa2);
}
//class- style custom2D shape creation
private void button2_Click(object sender, RoutedEventArgs e)
{
myCustomAnimatedBox MB = new myCustomAnimatedBox(200, 200, 350, 250) { Stroke = Brushes.Black, StrokeThickness = 2 };
canvas1.Children.Add(MB); //new myCustomAnimatedBox(200, 200, 300, 250,500,500){Stroke = Brushes.Black, StrokeThickness = 2});
//PointAnimation pa = new PointAnimation();
////DoubleAnimation da = new DoubleAnimation();
//pa.To = new Point(100, 100);
////da.By = 50;
//pa.Duration = TimeSpan.FromSeconds(10);
//MB.BeginAnimation(myCustomAnimatedBox.CenterProperty, pa);
}
}
//Class that defines the custom 2D shape
public class myCustomAnimatedBox : Shape
{
private double x1, width;
private double y1, height;
private GeometryGroup myBox = new GeometryGroup();
public myCustomAnimatedBox(double XX1, double YY1, double widTH, double heiGHT)
{
makeBox(XX1, YY1, widTH, heiGHT);
}
//public static readonly DependencyProperty CenterProperty = DependencyProperty.Register("Center", typeof(Point), typeof(myCustomAnimatedBox),
// new PropertyMetadata( null,PropertyChanged));// FrameworkPropertyMetadata( FrameworkPropertyMetadataOptions.AffectsMeasure));
////new Point(cent_X, cent_Y)
//public Point Center
//{
// get { return (Point)GetValue(CenterProperty); }
// set
// {
// SetValue(CenterProperty, value);
// myBox.Children.Clear();
// makeBox(myBox, value.X, value.Y, width, height);
// }
//}
//public Point Center
//{
// get { return (Point)GetValue(CenterProperty); }
// set
// {
// SetValue(CenterProperty, value);
// myBox.Children.Clear();
// makeBox(myBox, value.X, value.Y, width, height);
// }
//}
//public Point Top
//{
// get { return new Point(X1, Y1); }
// set
// {
// //X1 = value.X;
// //Y1 = value.Y;
// Top = value;
// }
//}
//public static readonly DependencyProperty TopProperty = DependencyProperty.Register("Top", typeof(Point), typeof(myCustomAnimatedBox), new FrameworkPropertyMetadata( FrameworkPropertyMetadataOptions.AffectsMeasure));
//new PropertyMetadata( null,PropertyChanged));
public double X1
{
get { return x1; }
set
{
x1 = value;
}
}
public double Y1
{
get { return y1; }
set
{
y1 = value;
}
}
//Trial at creating a dependency property
//private static void TopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
//{
// myCustomAnimatedBox myB = (myCustomAnimatedBox)d;
// myB.InvalidateVisual();
//}
protected override Geometry DefiningGeometry
{
get { return myBox; }
}
//Function that draws the object
private void makeBox(double XX1, double YY1, double widTH, double heiGHT)
{
width = widTH;
height = heiGHT;
this.X1 = XX1;
this.Y1 = YY1;
RectangleGeometry rectangle_part = new RectangleGeometry();
rectangle_part.Rect = new Rect(X1,Y1,width,height);
RectangleGeometry rg_cover = new RectangleGeometry();
RectangleGeometry rg_top = new RectangleGeometry();
rg_top.Rect = new Rect(X1, Y1 - 10, width, 2);
rg_cover.Rect = new Rect(X1, Y1, width, 5);
myBox.Children.Add(rectangle_part);
myBox.Children.Add(rg_top);
myBox.Children.Add(rg_cover);
}
}
}
名称空间Wpftrial4
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
SolidColorBrush fillcolor=新的SolidColorBrush();
SolidColorBrush bordercolor=新的SolidColorBrush();
公共主窗口()
{
初始化组件();
fillcolor.Color=Colors.WhiteSmoke;
bordercolor.Color=Colors.Blue;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
//手动创建所需的二维长方体形状
路径路径_矩形=新路径();
path_rectangle.Fill=fillcolor;
path_rectangle.Stroke=边框颜色;
路径_矩形.StrokeThickness=5;
矩形几何rg=新矩形几何();
双倍宽度_rec=100;
双高_rec=50;
左双_rec=130;
双层顶部_rec=100;
rg.Rect=新的Rect(左rec、上rec、宽rec、高rec);
path_rectangle.Data=rg;
canvas1.Children.Add(路径\矩形);
路径路径2_top=新路径();
path2_top.Stroke=新的SolidColorBrush(Colors.Brown);
路径2_top.StrokeThickness=6;
矩形几何体rg2=新矩形几何体();
双倍宽度2=100;
双高2=2;
双左2=130;
双top2=90;
rg2.Rect=新的Rect(left2,top2,width2,height2);
路径2_top.Data=rg2;
canvas1.Children.Add(路径2_顶部);
路径路径3_topcover=新路径();
path3_topcover.Stroke=新的SolidColorBrush(Colors.White);
路径3_topcover.StrokeThickness=5;
矩形几何体rg3=新矩形几何体();
双倍宽度3=100;
双高3=5;
双左3=130;
双top3=100;
rg3.Rect=新的Rect(left3,top3,width3,height3);
路径3_topcover.Data=rg3;
画布1.儿童。添加(路径3_顶盖);
//组合部件组类型
GeometryGroup myGeometryGroup=新的GeometryGroup();
myGeometryGroup.Children.Add(rg);
myGeometryGroup.Children.Add(rg3);
路径myPath=新路径();
myPath.Stroke=画笔.Blue;
myPath.Data=myGeometryGroup;
canvas1.Children.Add(myPath);
////测试动画
//myCustomAnimatedBox MB2=新的myCustomAnimatedBox(200200300250){笔划=画笔。黑色,笔划宽度=2};
//canvas1.Children.Add(MB2);//新建myCustomAnimatedBox(200200300250500500){Stroke=brush.Black,StrokeThickness=2});
//PointAnimation pa2=新的PointAnimation();
////DoubleAnimation da=新的DoubleAnimation();
//pa2.To=新点(100100);
////da.By=50;
//pa2.持续时间=从秒开始的时间跨度(10);
////MB2.BeginAnimation(myCustomAnimatedBox.TopProperty,第2页);//(///MB2.BeginAnimation(myCustomAnimatedBox.CenterProperty,第2页);
}
//类-样式自定义二维形状创建
私有无效按钮2\u单击(对象发送者,路由目标)
{
myCustomAnimatedBox MB=新的myCustomAnimatedBox(200200350250){笔划=画笔。黑色,笔划宽度=2};
canvas1.Children.Add(MB);//新建myCustomAnimatedBox(200200300250500500){Stroke=brush.Black,StrokeThickness=2});
//PointAnimation pa=新的PointAnimation();
////DoubleAnimation da=新的DoubleAnimation();
//pa.To=新点(100100);
////da.By=50;
//pa.持续时间=从秒开始的时间跨度(10);
//MB.BeginAnimation(myCustomAnimatedBox.CenterProperty,pa);
}
}
//类,该类定义自定义二维形状
公共类myCustomAnimatedBox:形状
{
私有双x1,宽度;
私人双y1,高;
private GeometryGroup myBox=新建GeometryGroup();
公共myCustomAnimatedBox(双XX1、双YY1、双宽、双高)
{
制作盒(XX1,YY1,宽度,高度);
}
//public static readonly dependencProperty CenterProperty=dependencProperty.Register(“中心”、typeof(点)、typeof(myCustomAnimatedBox),
//新建PropertyMetadata(null,PropertyChanged));//FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.AffectsMeasure));
////新点(分X、分Y)
//公共点中心
//{
//获取{return(Point)GetValue(CenterProperty);}
//设置
// {
//SetValue(CenterProperty,值);
//myBox.Children.Clear();
//makeBox(myBox,value.X,value.Y,宽度,高度);
// }
//}
//公共点中心
//{
//获取{return(Point)GetValue(CenterProperty);}
//设置
// {
//SetValue(CenterProperty,值);
//myBox.Children.Clear();
//makeBox(myBox,value.X,value.Y,宽度,高度);
// }
//}
//公共点顶部
//{
//获取{返回新点(X1,Y1);}
//设置
// {
////X1=值.X;
////Y1=value.Y;
//Top=价值;
// }
//}
//public static readonly dependencProperty TopProperty=dependencProperty.Register(“Top”、typeof(Point)、typeof(myCustomAnimatedBox)、new FrameworkPropertyMetadata(FrameworkPropertyMetadata