WPF基于3D对象捕获鼠标点击事件
ModelUIElment3D
UIElment3D是.NET3.5新增的类,替代了ModelVisual3D。ModelVisual3D有2个派生类ModelUIElment3D和ContainerUIElment3D。ModelUIElment3D跟ModelVisual3D最大的不同就是,不需要使用HItTest来检测鼠标点击的某个具体的3D对象模型。取而代之是标准2D事件名MouseLeftButtonDown。
新建WPF项目
打开Vs2015,新建一个WPF项目工程,新建一个窗体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>此处是模型数据,省略,一般由ZAM3D绘制或者3dmaxobj导入自动生成。。
</GeometryModel3D.Geometry>
</GeometryModel3D>
</ModelUIElement3D.Model>
</ModelUIElement3D>
</Viewport3D.Children>
编写鼠标响应事件代码
private void model_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(“点击的是1”);
}