目标:

WPF学习系列 绘制旋转的立方体

中间的方块会不停的旋转。

第二步,为xaml窗体布局

下面是源代码(不是我写的)

WPF学习系列 绘制旋转的立方体

先给grid设置背景颜色: Background="Black"

然后拖一个ContentControl到窗体上,默认的contentcontrol为

WPF学习系列 绘制旋转的立方体

WPF学习系列 绘制旋转的立方体

删掉这些属性后后,宽高就自动变成100%了。然后将单标签改为双标签。

contentcontrol中间有个Viewport3D标签,在工具箱里找了下,纳尼,没有?这不科学!

WPF学习系列 绘制旋转的立方体

网上关于Viewport3D的教程还是很多,但是没有人告诉我他们是怎么拖进去的,只能,手写了。

用WinForm习惯了拖控件,用WPF很不习惯,WPF更倾向于Web的编程布局方式,优势是界面控制性更强,缺点是更难掌握,WPF更适用于对前端样式有更多需求的应用。

WPF学习系列 绘制旋转的立方体

ClipToBounds:当设置ClipToBounds为True时,超出部分一定会被裁剪掉;ClipToBounds为False时,超出部分不一定不会被裁剪掉

窗体布局到这里就完了。下面是我的代码,我的原则是感觉没有用的东西就不要写,等它报错。所以很多属性我都删掉了。

 

第三步 将Viewport3D 属性loaded中的Viewport3D_Loaded写出来

这段代码的作用是绘制viewport3D中的画面。也是这段程序的主体部分

下面是源代码(不是我写的)

WPF学习系列 绘制旋转的立方体

 先翻译一下

WPF学习系列 绘制旋转的立方体

业务逻辑大概就是这样,然后补充相关的代码,让它先能跑起来。

先建一个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;
        }
    }
WpfCube.cs

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-06-07
  • 2022-02-07
  • 2021-06-22
  • 2022-01-06
猜你喜欢
  • 2021-11-29
  • 2021-11-08
  • 2022-12-23
  • 2022-02-14
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案