原文: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. 【计算机】基本概念的理解 —— 沙盒(sandbox)、交互式计算/编程/应用

    web scraper:网络铲: scraper:n. 刮刀:铲土机:守财奴: 1. 交互式计算/编程/应用(interactive computing/application/programming ...

  2. 认识PWA

    原文 简书原文:https://www.jianshu.com/p/f38f21ed45dc 大纲 前言 1.什么是PWA 2.PWA 应该具备的特点 3.PWA基础 4.构建 PWA 的业务场景 5 ...

  3. fastjson排序 Map多层嵌套转换自动排序问题终极解决方案

    阅读更多 最近项目中用到了fastjson(1.2.15)需要将前端多层嵌套json转换为map,由于map的无序性,想了很多办法,最终找到使用 Map m= JSONArray.parseObjec ...

  4. 卸载、指定卸载 .NET Core Runtime and SDK

    原文:卸载.指定卸载 .NET Core Runtime and SDK 项目使用的 Nuget 包,比如 Microsoft.AspNetCore.App等的版本号要与 .NET Core 版本号( ...

  5. 虚幻引擎中的数组---TArray: Arrays

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45367171 作者:ca ...

  6. [Angular Directive] 3. Handle Events with Angular 2 Directives

    A @Directive can also listen to events on their host element using @HostListener. This allows you to ...

  7. [React Router v4] Intercept Route Changes

    If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm ...

  8. [React] Public Class Fields with React Components

    Public Class Fields allow you to add instance properties to the class definition with the assignment ...

  9. 如何在PHP页面中原样输出HTML代码(是该找本php的数来看了)

    如何在PHP页面中原样输出HTML代码(是该找本php的数来看了) 一.总结 一句话总结:字符串与HTML之间的相互转换主要应用htmlentities()函数来完成. 1.php中的html标签如何 ...

  10. 【t077】宝物筛选

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物--这下小FF可发财了.但是这里的宝物实在是太多 ...