如何旋转 WPF 窗口?

发布于 2024-11-02 01:24:48 字数 35 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-11-09 01:24:48

第一个问题:为什么要旋转整个窗口?

如果您确实需要它:

您无法旋转普通的 WPF 窗口。请参阅:旋转窗口< /a>

您必须创建一个无边框窗口并为其提供 UI。请参阅:自定义窗口框架的 WPF 非客户端区域设计技术

对于旋转窗口外观:

将:

  • AllowTransparency 属性设置为
    真的。
  • WindowStyle 为 None 为
    删除窗口镀铬
  • 背景
    透明

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

  • 白色背景(或任何非透明颜色)
  • 旋转变换和
  • 较小的尺寸(以便在窗口内旋转时适合)。

Border 将为您的窗口提供 UI。

请注意创建自己的无边框窗口的注意事项,因为它需要您提供窗口界面,例如最小化、最大化、关闭按钮;并且可能需要一些非托管代码。

另外,在下面的示例代码中,旋转时的边框必须保持在窗口的边界内,否则它将被修剪(以及您的自定义窗口)。

示例代码

<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>

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

For rotated window look:


Set:

  • AllowTransparency property to
    true.
  • WindowStyle to None to
    remove window chrome
  • 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.



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.

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>
你与清晨阳光 2024-11-09 01:24:48

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

示例(有点简单):

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

——丹

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.

Example (somewhat simple):

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

-- Dan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文