如何旋转WPF窗口?

时间:2022-11-13 10:34:37

Is it possible to rotate a WPF Window by 45 degree, using xaml?

是否可以使用xaml将WPF窗口旋转45度?

2 个解决方案

#1


7  

First question: Why do you want to rotate the whole window?

If you really need it:
You can't rotate the normal WPF window. See: Rotate Window

You will have to create a borderless window and provide a UI to it. See: WPF Non-Client Area Design Techniques For Custom Window Frames

第一个问题:为什么要旋转整个窗口?如果你真的需要它:你不能旋转正常的WPF窗口。请参阅:旋转窗口您必须创建无边框窗口并为其提供UI。请参阅:自定义窗口框架的WPF非客户区设计技术

For rotated window look:
Set:

对于旋转窗口外观:设置:

  • AllowTransparency property to true.
  • AllowTransparency属性为true。
  • WindowStyle to None to remove window chrome
  • WindowStyle为None以删除窗口镶边
  • Background to Transparent
  • 透明的背景

Include a border (or anything meaningful like rectangle, circle, ellipse, etc.) as content of the window and following properties of border:

包含边框(或任何有意义的内容,如矩形,圆形,椭圆等)作为窗口的内容和边框的以下属性:

  • white background (or any non-transparent color)
  • 白色背景(或任何不透明的颜色)
  • rotate transformation, and
  • 旋转变换,和
  • smaller size (so as to fit when rotated within the window).
  • 较小的尺寸(以便在窗户内旋转时适合)。

Border will provide the UI to your window.

Border将为您的窗口提供UI。


Be aware of cavaets of creating own borderless window, as it requires you to provide the window interface like minimise, maximise, close buttons; and may require some unmanaged code.
Also, in sample code below, the border when rotated has to be kept within the bounds of the window, otherwise it (and your custom window) will be trimmed.

注意创建自己的无边框窗口的cavaets,因为它要求你提供窗口界面,如最小化,最大化,关闭按钮;并且可能需要一些非托管代码。此外,在下面的示例代码中,旋转时的边框必须保持在窗口的边界内,否则它(和您的自定义窗口)将被修剪。

Sample code

示例代码

<Window x:Class="CustomWindowStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        AllowsTransparency="True" WindowStyle="None" Background="Transparent"
        Title="MainWindow" Height="600" Width="600">

        <Border BorderBrush="Green" BorderThickness="2" Background="White" Width="360" Height="360">
            <Border.RenderTransform>
                <RotateTransform Angle="-45" CenterX="180" CenterY="180"/>
            </Border.RenderTransform>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="23" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Button Grid.Row="0" Content="X" Height="23" Width="23" Name="button1" HorizontalAlignment="Right" VerticalAlignment="Top" Click="button1_Click"/>
                <Grid Grid.Row="1">
                    <!--Main window content goes here-->
                    <TextBlock Text="Main window content goes here" HorizontalAlignment="Center" />
                </Grid>
            </Grid>
        </Border>
</Window>

#2


1  

As far as I know you can't rotate an entire window, but you could put everything inside the window into a custom control and apply apply a RenderTransform object to the custom control.

据我所知,您不能旋转整个窗口,但您可以将窗口内的所有内容放入自定义控件并应用RenderTransform对象应用于自定义控件。

Example (somewhat simple):

示例(有点简单):

http://www.codeproject.com/KB/WPF/TransformationsIntro.aspx

http://www.codeproject.com/KB/WPF/TransformationsIntro.aspx

-- Dan

- 丹

#1


7  

First question: Why do you want to rotate the whole window?

If you really need it:
You can't rotate the normal WPF window. See: Rotate Window

You will have to create a borderless window and provide a UI to it. See: WPF Non-Client Area Design Techniques For Custom Window Frames

第一个问题:为什么要旋转整个窗口?如果你真的需要它:你不能旋转正常的WPF窗口。请参阅:旋转窗口您必须创建无边框窗口并为其提供UI。请参阅:自定义窗口框架的WPF非客户区设计技术

For rotated window look:
Set:

对于旋转窗口外观:设置:

  • AllowTransparency property to true.
  • AllowTransparency属性为true。
  • WindowStyle to None to remove window chrome
  • WindowStyle为None以删除窗口镶边
  • Background to Transparent
  • 透明的背景

Include a border (or anything meaningful like rectangle, circle, ellipse, etc.) as content of the window and following properties of border:

包含边框(或任何有意义的内容,如矩形,圆形,椭圆等)作为窗口的内容和边框的以下属性:

  • white background (or any non-transparent color)
  • 白色背景(或任何不透明的颜色)
  • rotate transformation, and
  • 旋转变换,和
  • smaller size (so as to fit when rotated within the window).
  • 较小的尺寸(以便在窗户内旋转时适合)。

Border will provide the UI to your window.

Border将为您的窗口提供UI。


Be aware of cavaets of creating own borderless window, as it requires you to provide the window interface like minimise, maximise, close buttons; and may require some unmanaged code.
Also, in sample code below, the border when rotated has to be kept within the bounds of the window, otherwise it (and your custom window) will be trimmed.

注意创建自己的无边框窗口的cavaets,因为它要求你提供窗口界面,如最小化,最大化,关闭按钮;并且可能需要一些非托管代码。此外,在下面的示例代码中,旋转时的边框必须保持在窗口的边界内,否则它(和您的自定义窗口)将被修剪。

Sample code

示例代码

<Window x:Class="CustomWindowStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        AllowsTransparency="True" WindowStyle="None" Background="Transparent"
        Title="MainWindow" Height="600" Width="600">

        <Border BorderBrush="Green" BorderThickness="2" Background="White" Width="360" Height="360">
            <Border.RenderTransform>
                <RotateTransform Angle="-45" CenterX="180" CenterY="180"/>
            </Border.RenderTransform>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="23" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Button Grid.Row="0" Content="X" Height="23" Width="23" Name="button1" HorizontalAlignment="Right" VerticalAlignment="Top" Click="button1_Click"/>
                <Grid Grid.Row="1">
                    <!--Main window content goes here-->
                    <TextBlock Text="Main window content goes here" HorizontalAlignment="Center" />
                </Grid>
            </Grid>
        </Border>
</Window>

#2


1  

As far as I know you can't rotate an entire window, but you could put everything inside the window into a custom control and apply apply a RenderTransform object to the custom control.

据我所知,您不能旋转整个窗口,但您可以将窗口内的所有内容放入自定义控件并应用RenderTransform对象应用于自定义控件。

Example (somewhat simple):

示例(有点简单):

http://www.codeproject.com/KB/WPF/TransformationsIntro.aspx

http://www.codeproject.com/KB/WPF/TransformationsIntro.aspx

-- Dan

- 丹