WPF旋转的界面实现

本文介绍了如何在WPF中创建旋转的界面效果,通过XAML代码实现,既美观又节省资源。
摘要由CSDN通过智能技术生成

在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>
                            <Viewport2DVi
实现界面上画旋转矩形,可以使用 WPF 的 `Canvas` 控件和 `RotateTransform` 类型。 首先,在 XAML 中创建一个 `Canvas` 控件,用于显示画布,以及一个按钮控件,用于触发画矩形的操作。 ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <Button Click="DrawRectangle" Content="Draw Rectangle"/> <Canvas x:Name="canvas" Background="White" Width="500" Height="300"/> </StackPanel> </Window> ``` 在代码中,定义一个 `bool` 类型的变量 `isDrawing`,用于标识当前是否正在画矩形。 ```csharp bool isDrawing = false; ``` 在按钮的点击事件 `DrawRectangle` 中,设置 `isDrawing` 为 `true`,并在 `MouseMove` 事件中根据鼠标移动的位置动态调整矩形的大小和位置。 ```csharp private void DrawRectangle(object sender, RoutedEventArgs e) { isDrawing = true; canvas.MouseMove += OnMouseMove; } private void OnMouseMove(object sender, MouseEventArgs e) { if (!isDrawing) return; Point startPoint = e.GetPosition(canvas); // 计算矩形的宽度和高度 double width = Math.Abs(startPoint.X - endPoint.X); double height = Math.Abs(startPoint.Y - endPoint.Y); // 计算旋转角度 double angle = Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) * 180 / Math.PI; // 设置矩形的位置和大小 Canvas.SetLeft(rectangle, Math.Min(startPoint.X, endPoint.X)); Canvas.SetTop(rectangle, Math.Min(startPoint.Y, endPoint.Y)); rectangle.Width = width; rectangle.Height = height; // 设置矩形的旋转角度 rectangle.RenderTransformOrigin = new Point(0.5, 0.5); rectangle.RenderTransform = new RotateTransform(angle); } private void OnMouseUp(object sender, MouseButtonEventArgs e) { isDrawing = false; canvas.MouseMove -= OnMouseMove; } ``` 在 `MouseMove` 事件中,首先获取鼠标当前的位置 `startPoint`,然后根据鼠标移动的位置计算出矩形的宽度和高度。接着,使用 `Math.Atan2` 方法计算出矩形的旋转角度 `angle`,并使用 `Canvas.SetLeft`、`Canvas.SetTop`、`Width` 和 `Height` 属性设置矩形的位置和大小。最后,使用 `RotateTransform` 对象设置矩形的旋转角度。 在代码中,还需要定义一个 `Rectangle` 控件,并在 `OnMouseMove` 方法中动态设置其位置、大小和旋转角度。 ```csharp Rectangle rectangle = new Rectangle { Stroke = Brushes.Black, StrokeThickness = 2 }; canvas.Children.Add(rectangle); ``` 最后,在 `OnMouseUp` 事件中,将 `isDrawing` 设置为 `false`,并取消 `MouseMove` 事件的订阅,以结束画矩形的操作。 完整的代码如下: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <Button Click="DrawRectangle" Content="Draw Rectangle"/> <Canvas x:Name="canvas" Background="White" Width="500" Height="300"/> </StackPanel> </Window> ``` ```csharp bool isDrawing = false; Point endPoint; Rectangle rectangle = new Rectangle { Stroke = Brushes.Black, StrokeThickness = 2 }; public MainWindow() { InitializeComponent(); canvas.Children.Add(rectangle); canvas.MouseUp += OnMouseUp; } private void DrawRectangle(object sender, RoutedEventArgs e) { isDrawing = true; canvas.MouseMove += OnMouseMove; } private void OnMouseMove(object sender, MouseEventArgs e) { if (!isDrawing) return; Point startPoint = e.GetPosition(canvas); // 计算矩形的宽度和高度 double width = Math.Abs(startPoint.X - endPoint.X); double height = Math.Abs(startPoint.Y - endPoint.Y); // 计算旋转角度 double angle = Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) * 180 / Math.PI; // 设置矩形的位置和大小 Canvas.SetLeft(rectangle, Math.Min(startPoint.X, endPoint.X)); Canvas.SetTop(rectangle, Math.Min(startPoint.Y, endPoint.Y)); rectangle.Width = width; rectangle.Height = height; // 设置矩形的旋转角度 rectangle.RenderTransformOrigin = new Point(0.5, 0.5); rectangle.RenderTransform = new RotateTransform(angle); } private void OnMouseUp(object sender, MouseButtonEventArgs e) { isDrawing = false; canvas.MouseMove -= OnMouseMove; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值