原文:WPF旋转的界面实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyisen0713/article/details/18215349

在WPF中可以做出旋转的界面,这样不仅效果好看,而且节省界面和代码处理。

xaml代码如下:

<Window x:Class="V3ViewApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="旋转界面" Height="360" Width="450" Background="Black" WindowState="Normal" WindowStartupLocation="CenterScreen">
<Window.Resources>
<sys:Double x:Key="grdSize">200</sys:Double>
</Window.Resources>
<Grid >
<Viewport3D ClipToBounds="True" HorizontalAlignment="Center" VerticalAlignment="Center" Height="500" Width="500">
<Viewport3D.Camera>
<PerspectiveCamera LookDirection="0,0,-1" Position="0,0,300"/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>
<ModelVisual3D.Content>
<AmbientLight Color="Transparent"/>
</ModelVisual3D.Content>
<ModelVisual3D.Children> <Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-50,50,-50 -50,-50,-50 -50,-50,50 -50,50,50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Yellow" Width="{StaticResource grdSize}" Height="{StaticResource grdSize}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D> <Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-50,50,50 -50,-50,50 50,-50,50 50,50,50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Red" Height="{StaticResource grdSize}" Width="{StaticResource grdSize}">
<StackPanel Margin="4"> </StackPanel>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D> <Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="50,50,50 50,-50,50 50,-50,-50 50,50,-50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Green" Width="{StaticResource grdSize}" Height="{StaticResource grdSize}">
<Canvas>
<Ellipse />
</Canvas>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D> <Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="50,50,-50 50,-50,-50 -50,-50,-50 -50,50,-50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Blue" Height="{StaticResource grdSize}" Width="{StaticResource grdSize}"> </Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
</ModelVisual3D.Children>
<ModelVisual3D.Transform>
<RotateTransform3D CenterX="0" CenterY="0" CenterZ="0">
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="0" Axis="0,1,0" x:Name="ar"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
</ModelVisual3D>
</Viewport3D.Children>
</Viewport3D>
</Grid>
</Window>

CS代码如下:

  public MainWindow()
{
InitializeComponent(); // 设置快捷键
KeyBinding forwBind = new System.Windows.Input.KeyBinding();
forwBind.Command = new ForwCommand();
forwBind.CommandParameter = ar;
forwBind.Key = System.Windows.Input.Key.Right;
this.InputBindings.Add(forwBind);
KeyBinding backBind = new System.Windows.Input.KeyBinding();
backBind.Command = new BackCommand();
backBind.CommandParameter = ar;
backBind.Key = System.Windows.Input.Key.Left;
this.InputBindings.Add(backBind); this.Loaded += (k, k2) =>
{ };
}
} /// <summary>
/// 向前移动
/// </summary>
public class ForwCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (parameter == null)
{
return false;
}
return true;
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
AxisAngleRotation3D rot = parameter as AxisAngleRotation3D;
DoubleAnimation d = new DoubleAnimation();
d.Duration = new Duration(TimeSpan.FromMilliseconds(800));
d.By = 90d;
rot.BeginAnimation(AxisAngleRotation3D.AngleProperty, d, HandoffBehavior.Compose);
}
} /// <summary>
/// 向后移动
/// </summary>
public class BackCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (parameter == null)
{
return false;
}
return true;
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
AxisAngleRotation3D rot = parameter as AxisAngleRotation3D;
DoubleAnimation d = new DoubleAnimation();
d.By = -90d;
d.Duration = new Duration(TimeSpan.FromMilliseconds(800));
rot.BeginAnimation(AxisAngleRotation3D.AngleProperty, d, HandoffBehavior.Compose);
}
}

最后用键盘左右键实现界面切换,之后可以自己改成别的快捷键进行切换。

效果如图:

WPF旋转的界面实现的更多相关文章

  1. Wix 安装部署教程(九) --用WPF做安装界面

    经常安装PC端的应用,特别是重装系统之后,大致分为两类.一类像QQ,搜狗输入法这样的.分三步走的:第一个页面可以自定义安装路径和软件许可.第二个页面显示安装进度条,第三个页面推荐其他应用.先不管人家怎 ...

  2. WPF模拟雷达界面效果图

    原文:WPF模拟雷达界面效果图 iPad塔防的防守兵的效果很炫,2个小时用WPF模拟了一个. 效果图: 关键代码: <Grid> <Grid.Background> <I ...

  3. wpf中防止界面卡死的写法

    原文:wpf中防止界面卡死的写法 ); this.Dispatcher.BeginInvoke(new Action(() => { this.button1.Content = "计 ...

  4. C# WPF 一个设计界面

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. C# WPF 一个设计界面 今天正月初三,大家在家呆着挺好,不要忘了自我充电. 武汉人民加油, ...

  5. WPF 显示初始化界面

    今天在看<WPF编程宝典>时,看到了Application类,该类可以做很多事情,我认为比较实用的是显示初始化界面,因为之前有个项目在打开的时候要加载好多dll,非常耗时,让客户等的蛋疼, ...

  6. WPF设置软件界面背景为MediaElement并播放视频

    在我们的常见的软件界面设计中我们经常会设置软件的背景为SolidColorBrush或者LinerColorBrush.RadialGradientBrush 等一系列的颜色画刷为背景,有时我们也会使 ...

  7. WPF开发的界面调用C++生成的dll文件

    以引用d1.dll为例. [生成d1.dll] 文件——新建——项目——Visual C++——Win32项目,选择DLL,点击Finish.在d1.cpp中添加代码 #include "s ...

  8. 【WPF/WAF】界面布局(View)文件的多层嵌套(Nest)

    碎碎念:使用的是略冷门的Window Application Foundation(WAF)框架,搜到的都是WPF的UserControl用户控件的用法,实在蛋疼. 需求:主界面ShellWindow ...

  9. [WPF]绑定到界面的数组不支持调度线程以外对其更改的办法

    [原]WPF编程经常遇到一个问题: 某个数组己绑定到主界面某控件中,然后在后台程序中需要对数组增(减)数据,然后程序就会报错, 程序提示:该类型的CollectionView 不支持从调度程序线程以外 ...

随机推荐

  1. Hbase常见异常 分类: B7_HBASE 2015-02-02 16:16 412人阅读 评论(0) 收藏

    1. HBase is able to connect to ZooKeeper but the connection closes immediately hbase(main):001:0> ...

  2. FOJ (FZU) 1476 矩形的个数 排列组合。

    http://acm.fzu.edu.cn/problem.php?pid=1476  Problem Description 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1* ...

  3. Ubuntu12.04LTS SDK无法更新

    1.打开终端输入sudo gedit /etc/hosts 加入下面 2.加入下列文字到文件里:       203.208.46.146 dl.google.com       203.208.46 ...

  4. Spring-boot更改成war包的方式

    转载至:  https://blog.csdn.net/zhuwei_clark/article/details/82114102  Step1 修改启动类 Step2 修改配置文件为properti ...

  5. HDU4876:ZCC loves cards

    Problem Description ZCC loves playing cards. He has n magical cards and each has a number on it. He ...

  6. css选择器指定元素中第几个子元素

    tr td:nth-child(2){ background-color:gray; } 就是tr当中的td的第二个td的属性 tr:nth-child(2n+0){ background-color ...

  7. 华为上机试题(java)

    一.题目描述:通过键盘输入一串小写字母(a~z)组成的字符串.请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉.比如字符串“abacacde”过滤结果为“abcde”. ...

  8. [React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

    If you’ve created several Routes within your application, you will also want to be able to navigate ...

  9. [Recompose] When nesting affects Style

    In CSS we use the descendant selector to style elements based on their nesting. Thankfully in React ...

  10. 稀疏编码(sparse code)与字典学习(dictionary learning)

    Dictionary Learning Tools for Matlab. 1. 简介 字典 D∈RN×K(其中 K>N),共有 k 个原子,x∈RN×1 在字典 D 下的表示为 w,则获取较为 ...