如何仅从顶部调整无边框窗口的大小? [英] How to resize a borderless window from top only?

查看:71
本文介绍了如何仅从顶部调整无边框窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WPF 应用程序,并希望我的窗口无边框,并且还可以从顶部调整大小.

到目前为止我尝试了什么

  • 我最初认为这会奏效:

    <网格背景=灰色"/></窗口>

    确实得到一个只有顶部边框的窗口,但我无法调整它的大小.

  • 然后我尝试了 WindowChrome.ResizeGripDirection="Top"ResizeMode="CanResizeWithGrip".

    <窗口...WindowStyle="无"ResizeMode="CanResizeWithGrip"允许透明度 =真"WindowChrome.ResizeGripDirection="顶部"BorderThickness="0,5,0,0"BorderBrush="黑色">...</窗口>

    这也不起作用(无法从顶部边框调整大小),并且手柄甚至没有出现在顶部.它停留在右下角(不过我可以用手柄调整大小).

  • 这可以从各个方向调整大小.我希望它能够从顶部调整大小.(惊讶:我什至没有将新的 chrome 对象分配给任何东西.它是如何工作的?我猜这是另一个问题.

<小时>

问题

如何制作无边框窗口,该窗口可以使用顶部边框?(最好只用一个颜色可以改变的顶部边框来做到这一点).

解决方案

您可能已成功设置 WindowChrome.ResizeBorderThickness 属性以移除除顶部之外的所有边框,例如ResizeBorderThickness="0, 5, 0, 0".

这可能不是获得结果的最干净的方法,但我已经成功地调整了这里的答案:http://www.eidias.com/blog/2014/1/27/restyle-your-window(这是我发现获得 <代码>WindowChrome 工作):

ResourceDictionary 中创建自定义窗口样式:

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"><Setter Property="WindowChrome.WindowChrome"><Setter.Value><WindowChrome CaptionHeight="30"角半径=4"GlassFrameThickness="0"ResizeBorderThickness="0, 5, 0, 0"UseAeroCaptionButtons="False"/></Setter.Value></Setter><Setter Property="Window.BorderThickness" Value="0, 5, 0, 0"/</风格></ResourceDictionary>

在需要的地方引用字典(我把它放在 App.xaml 中):

<应用程序.资源><ResourceDictionary Source="WindowStyle.xaml"/></Application.Resources></应用程序>

在所需的Window中引用样式:

<网格></网格></窗口>

这应该会产生一个看起来像最后一个窗口的窗口,但只能从顶部调整大小(只能抓住顶部调整大小的手柄).

I am creating a WPF application and want my window to be borderless, and also possible to be resized only from the top.

What I have tried so far

  • I initially thought this would work:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Width="200" Height="150"
            WindowStyle="None"
            ResizeMode="CanResize"
            AllowsTransparency="True"
            BorderThickness="0,5,0,0"
            BorderBrush="Black">
        <Grid Background="Gray" />
    </Window>
    

    I do get a window with top border only, but I cannot resize it.

  • Then I tried WindowChrome.ResizeGripDirection="Top" with ResizeMode="CanResizeWithGrip".

    <Window ...
            WindowStyle="None"
            ResizeMode="CanResizeWithGrip"
            AllowsTransparency="True"
            WindowChrome.ResizeGripDirection="Top"
            BorderThickness="0,5,0,0"
            BorderBrush="Black">
        ...
    </Window>
    

    This does not work either (unable to resize from top border), and the grip does not even appear on top. It stays on the bottom-right corner (I can resize with the grip, though).

  • This answer seems like the answerer may initially have done this, but the code is unavailable.

  • This answer has a link to a blog post, I am not too eager to try it because I would like a solution without code behind.
  • And then there is this answer:

    • I get an error with this approach:

      <Window ...
              WindowStyle="None"
              ResizeMode="CanResizeWithGrip"
              AllowsTransparency="False">
          <Grid Background="Gray" />
          <Setter Property="WindowChrome.WindowChrome">
              <Setter.Value>
                  <WindowChrome CornerRadius="0"
                                GlassFrameThickness="1"
                                UseAeroCaptionButtons="False"/>
              </Setter.Value>
          </Setter>
      </Window>
      

      The property 'Content' is set more than once.

    • With code behind:

      <Window ...
              WindowStyle="None"
              ResizeMode="CanResize"
              AllowsTransparency="False">
          <Grid Background="Gray" />
      </Window>
      

      In constructor:

      WindowChrome chrome = new WindowChrome();
      chrome.CornerRadius = new CornerRadius(0);
      chrome.GlassFrameThickness = new Thickness(0, 1, 0, 0);
      chrome.UseAeroCaptionButtons = false;
      

      Which gives me:

      And this could be resized from all directions. And I only want it to be able to resize from top. (Surprise: I did not even assign the new chrome object to anything. How did that work? That's another question I guess).


Question

How do I make a borderless window which can only be resized with the top border? (It's best if I can do this with only a top border whose color could be changed).

解决方案

You may have success setting the WindowChrome.ResizeBorderThickness property to remove all borders except the top, e.g. ResizeBorderThickness="0, 5, 0, 0".

It may not be the cleanest way to achieve your result, but I've had some success adapting the answer here: http://www.eidias.com/blog/2014/1/27/restyle-your-window (it was the easiest way I found to get WindowChrome working):

Create a custom window style in a ResourceDictionary:

<ResourceDictionary x:Class="WpfApplication.WindowStyle"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CaptionHeight="30"
                              CornerRadius="4"
                              GlassFrameThickness="0"
                              ResizeBorderThickness="0, 5, 0, 0"
                              UseAeroCaptionButtons="False" />
            </Setter.Value>
        </Setter>
        <Setter Property="Window.BorderThickness" Value="0, 5, 0, 0" /
    </Style>
</ResourceDictionary>

Reference the dictionary where required (I put it in App.xaml):

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="WindowStyle.xaml" />
    </Application.Resources>
</Application>

Reference the Style in the required Window:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    Style="{StaticResource ResourceKey=CustomWindowStyle}">
    <Grid>
    </Grid>    
</Window>

That should produce a window that looks like your final one, but can only be resized from the top (only the top resize handle can be grabbed).

这篇关于如何仅从顶部调整无边框窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆