Volkhard Vogeler

Volkhard Vogeler

  • NA
  • 11
  • 1.7k

WinForm Elementhost with WPF doesn´t show 3d Graphic

Aug 11 2020 12:14 PM
Hello,
i do have following Code on a winform:
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Windows.Forms.Integration;  
  4.   
  5. using wpf = System.Windows.Controls;  
  6. using wpfpaint = System.Windows.Media;  
  7. using System.Windows.Media.Media3D;  
  8.   
  9. namespace WindowsForms_WPF_3  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.   
  17.             FormBorderStyle = FormBorderStyle.Fixed3D;  
  18.               
  19.             // Elemethost für wpf-Control definieren  
  20.   
  21.             ElementHost eh = new ElementHost();  
  22.             eh.Dock = DockStyle.Fill;  
  23.             this.Controls.Add(eh);  
  24.   
  25.   
  26.             //wpf-Control DockPanel definieren und der Eigenschaft child des Elementhost zuordnen  
  27.   
  28.             wpf.DockPanel dp = new wpf.DockPanel();  
  29.             dp.Background = wpfpaint.Brushes.LightGray;  
  30.             eh.Child = dp;  
  31.   
  32.   
  33.             // wpf-Viewport3D Element für die Anzeige von 3D-Gfrafiken definieren  
  34.   
  35.             wpf.Viewport3D vp3d = new wpf.Viewport3D();  
  36.   
  37.   
  38.             // ...  
  39.   
  40.             wpfpaint.Media3D.ModelVisual3D visual3d = new wpfpaint.Media3D.ModelVisual3D();  
  41.             wpfpaint.Media3D.Model3DGroup group3d = new wpfpaint.Media3D.Model3DGroup();  
  42.   
  43.             visual3d.Content = group3d;  
  44.             vp3d.Children.Add(visual3d);  
  45.   
  46.   
  47.             // Kamera definieren  
  48.   
  49.             Point3D position = new Point3D(1, -1, 15);  
  50.             Vector3D lookDirection = new Vector3D(1, 1, 0);  
  51.             Vector3D upDirection = new Vector3D(0, 1, 0);  
  52.             double fieldOfView = 45;  
  53.             PerspectiveCamera camera = new PerspectiveCamera(position, lookDirection, upDirection, fieldOfView);  
  54.   
  55.             vp3d.Camera = camera;  
  56.   
  57.             // Licht definieren  
  58.   
  59.             group3d.Children.Add(new AmbientLight(wpfpaint.Colors.LightGreen));  
  60.             Vector3D direction = new Vector3D(1, -2, -3);  
  61.             group3d.Children.Add(new DirectionalLight(wpfpaint.Colors.LightSalmon, direction));  
  62.   
  63.             // Modell definieren  
  64.   
  65.             MeshGeometry3D mesh = new MeshGeometry3D();  
  66.             Point3D[] points =  
  67.             {  
  68.                 new Point3D(1, 1, 0), new Point3D(5, 1, 0), new Point3D(2.5, 5, 0)  
  69.             };  
  70.   
  71.             foreach (Point3D point in points) mesh.Positions.Add(point);  
  72.   
  73.             Tuple<intintint>[] triangles =  
  74.             {  
  75.                  new Tuple<intintint>(0, 1, 2)  
  76.             };  
  77.   
  78.             foreach (Tuple<intintint> tuple in triangles)  
  79.             {  
  80.                 mesh.TriangleIndices.Add(tuple.Item1);  
  81.                 mesh.TriangleIndices.Add(tuple.Item2);  
  82.                 mesh.TriangleIndices.Add(tuple.Item3);  
  83.             }  
  84.   
  85.             // Objektmaterial definieren  
  86.   
  87.             DiffuseMaterial material = new DiffuseMaterial(wpfpaint.Brushes.LightBlue);  
  88.   
  89.             // Modell kreieren  
  90.   
  91.             GeometryModel3D model = new GeometryModel3D(mesh, material);  
  92.   
  93.   
  94.             // Modell der Geometriegruppe zuweisen  
  95.   
  96.             group3d.Children.Add(model);  
  97.   
  98.             // wpf DockPanel zum Viewport 3D einfügen  
  99.   
  100.             dp.Children.Add(vp3d);  
  101.   
  102.         }  
  103.     }  
  104. }  

I expect that it Shows a cube but it doesn´t - When i do the simular thing within an wpf-program it works.
So i do have some Questions:
1) It is possible to use ElementHost to draw 3Ds in this way?
2) what is the failure in the Code above?
3) Is there the same ccordinate-System?
best regards
Volkhard

Answers (2)