WPF 3D object-based capture mouse click events

ModelUIElment3D
UIElment3D is .NET3.5 new category, replacing the ModelVisual3D. There are two ModelVisual3D derived classes ModelUIElment3D and ContainerUIElment3D. ModelUIElment3D with ModelVisual3D biggest difference is that, without the use of HItTest to detect a specific 3D object model mouse clicks. Instead, a standard 2D name of the event MouseLeftButtonDown.

New WPF project
open Vs2015, create a new WPF project, create a new form MainWindow.

设置背景颜色
<Grid.Background>
<DrawingBrush Viewport="0,0,0.05,0.05" TileMode="FlipXY">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="Black" Geometry="M0,0 L1,0 L1,1 L0,1" />
<GeometryDrawing Brush="DarkBlue" Geometry="M0,0.5 L0.5,0.5 L0.5,1 L0,1" />
<GeometryDrawing Brush="DarkBlue" Geometry="M0.5,0 L1,0 L1,0.5 L0.5,0.5" />
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Grid.Background>

增加 Viewport3D
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="0,0,7" LookDirection="0,0,-1"/>
</Viewport3D.Camera>
</Viewport3D>

增加ModelUIElement3D
<Viewport3D.Children>
<ModelUIElement3D x:Name="model" MouseLeftButtonDown="model_MouseLeftButtonDown" >
<ModelUIElement3D.Model>

<GeometryModel3D x:Name="Teapot">
<GeometryModel3D.Material>
<EmissiveMaterial Brush="Red" />
</GeometryModel3D.Material>

<GeometryModel3D.Geometry> where the model data is omitted, or generally drawn by ZAM3D 3dmaxobj generated automatically import. .
</GeometryModel3D.Geometry>
</ GeometryModel3D>

</ModelUIElement3D.Model>
</ ModelUIElement3D>
</Viewport3D.Children>

write response to the mouse event code
Private void model_MouseLeftButtonDown (Object SENDER, MouseButtonEventArgs E)
{
MessageBox.Show ( "click 1");
}

Guess you like

Origin www.cnblogs.com/lingzhihua/p/11125824.html