我是一年经验的web程序员,想学习一下wpf,比较喜欢做项目来学习,所以在网上找了一些项目,分析代码,尽量能够做到自己重新敲出来第一个项目是中间的方块会不停的旋转。第一步,新建wpf项目第二步,为xaml窗体布局下面是源代码(不是我写的)先给grid设置背景颜色: Backg ...
我是一年经验的web程序员,想学习一下get='_blank'>wpf,比较喜欢做项目来学习,所以在网上找了一些项目,分析代码,尽量能够做到自己重新敲出来
第一个项目是
中间的方块会不停的旋转。
第一步,新建wpf项目
第二步,为xaml窗体布局
下面是源代码(不是我写的)
先给grid设置背景颜色: Background="Black"
然后拖一个ContentControl到窗体上,默认的contentcontrol为
删掉这些属性后后,宽高就自动变成100%了。然后将单标签改为双标签。
contentcontrol中间有个Viewport3D标签,在工具箱里找了下,纳尼,没有?这不科学!
只能百度一下,关于Viewport3D的教程还是很多,但是没有人告诉我他们是怎么拖进去的,只能,手写了
纳尼,又报错!!仔细找了找我的是:<ContentControl Content="ContentControl"> 他的是:<ContentControl Name="contentControl2">
赶紧改改,果然对了
查了下ClipToBounds=“true”的意思:当你设置ClipToBounds为True时,超出部分一定会被裁剪掉;但当你设置ClipToBounds为False时,超出部分不一定不会被裁剪掉
窗体布局到这里就完了。下面是我的代码,我的原则是感觉没有用的东西就不要写,等它报错。所以很多属性我都删掉了
第三步 将Viewport3D 属性loaded中的Viewport3D_Loaded写出来
这段代码的作用是绘制viewport3D中的画面。也是这段程序的主体部分
下面是源代码(不是我写的)
先翻译一下
业务逻辑大概就是这样,然后补充相关的代码,让它先能跑起来。
先建一个WpfCube类,
using System.Windows.Media; using System.Windows.Media.Media3D; /// <summary> /// 这个类负责创建一个 立方体的 GeometryModel3D对象 /// </summary> public class WpfCube { private Point3D origin; private double width; private double height; private double depth; public Point3D centerBottom() { Point3D c = new Point3D( origin.X + (width / 2), origin.Y + height, origin.Z + (depth / 2) ); return c; } public Point3D center() { Point3D c = new Point3D( origin.X + (width / 2), origin.Y - height / 2, origin.Z + (depth / 2) ); return c; } public Point3D centerTop() { Point3D c = new Point3D( origin.X + (width / 2), origin.Y, origin.Z + (depth / 2) ); return c; } public WpfCube(Point3D P0, double w, double h, double d) { width = w; height = h; depth = d; origin = P0; } public WpfCube(WpfCube cube) { width = cube.width; height = cube.height; depth = cube.depth; origin = new Point3D(cube.origin.X, cube.origin.Y, cube.origin.Z); } public WpfRectangle Front() { WpfRectangle r = new WpfRectangle(origin, width, height, 0); return r; } public WpfRectangle Back() { WpfRectangle r = new WpfRectangle(new Point3D(origin.X + width, origin.Y, origin.Z + depth), -width, height, 0); return r; } public WpfRectangle Left() { WpfRectangle r = new WpfRectangle(new Point3D(origin.X, origin.Y, origin.Z + depth), 0, height, -depth); return r; } public WpfRectangle Right() { WpfRectangle r = new WpfRectangle(new Point3D(origin.X + width, origin.Y, origin.Z), 0, height, depth); return r; } public WpfRectangle Top() { WpfRectangle r = new WpfRectangle(origin, width, 0, depth); return r; } public WpfRectangle Bottom() { WpfRectangle r = new WpfRectangle(new Point3D(origin.X + width, origin.Y - height, origin.Z), -width, 0, depth); return r; } public static void addCubeToMesh(Point3D p0, double w, double h, double d, MeshGeometry3D mesh) { WpfCube cube = new WpfCube(p0, w, h, d); double maxDimension = Math.Max(d, Math.Max(w, h)); WpfRectangle front = cube.Front(); WpfRectangle back = cube.Back(); WpfRectangle right = cube.Right(); WpfRectangle left = cube.Left(); WpfRectangle top = cube.Top(); WpfRectangle bottom = cube.Bottom(); front.addToMesh(mesh); back.addToMesh(mesh); right.addToMesh(mesh); left.addToMesh(mesh); top.addToMesh(mesh); bottom.addToMesh(mesh); } public GeometryModel3D CreateModel(Color color) { return CreateCubeModel(origin, width, height, depth, color); } public static GeometryModel3D CreateCubeModel(Point3D p0, double w, double h, double d, Color color) { MeshGeometry3D mesh = new MeshGeometry3D(); addCubeToMesh(p0, w, h, d, mesh); Material material = new DiffuseMaterial(new SolidColorBrush(color)); GeometryModel3D model = new GeometryModel3D(mesh, material); return model; } }
原标题:WPF学习系列 绘制旋转的立方体
关键词:wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。