99

如果您ResizeMode="CanResizeWithGrip"在 WPF 上进行设置,Window则右下角会显示调整大小的夹点,如下所示:

如果您也设置WindowStyle="None"了,标题栏会消失,但灰色斜边仍然存在,直到您设置ResizeMode="NoResize". 不幸的是,在设置了这种属性组合后,调整大小的夹点也消失了。

我已经通过自定义覆盖了Window's 。我想自己指定窗口的边框,并且我不需要用户能够从所有四个侧面调整窗口的大小,但我确实需要调整大小的手柄。ControlTemplateStyle

有人可以详细说明满足所有这些标准的简单方法吗?

  1. 除了WindowControlTemplate.
  2. 右下角有一个有效的调整大小手柄。
  3. 没有标题栏。
4

5 回答 5

192

如果您在AllowsTransparency上设置属性Window(即使没有设置任何透明度值),边框也会消失,您只能通过夹点调整大小。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="640" Height="480" 
    WindowStyle="None"
    AllowsTransparency="True"
    ResizeMode="CanResizeWithGrip">

    <!-- Content -->

</Window>

结果如下:

于 2009-03-04T16:21:07.947 回答
97

我试图创建一个无边框窗口, WindowStyle="None"但是当我测试它时,顶部似乎出现了一个白条,经过一些研究,它似乎是“调整边框大小”,这是一张图片(我用黄色标记):

挑战

经过互联网的一些研究,以及许多困难的非 xaml 解决方案,我发现的所有解决方案都是 C# 中的代码和大量代码行,我在这里间接找到了解决方案:Maximum custom window lost drop shadow effect

<WindowChrome.WindowChrome>
    <WindowChrome 
        CaptionHeight="0"
        ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

注意 :您需要使用 .NET 4.5 框架,或者如果您使用的是旧版本,请使用 WPFShell,只需引用 shell 并Shell:WindowChrome.WindowChrome改用即可。

我使用了WindowChromeWindow 的属性,如果你使用它,白色的“调整边框”就会消失,但你需要定义一些属性才能正常工作。

CaptionHeight:这是标题区域(标题栏)的高度,允许像普通标题栏一样进行 Aero 捕捉、双击行为。将此设置为 0(零)以使按钮工作。

ResizeBorderThickness:这是窗口边缘的厚度,您可以在此处调整窗口大小。我放 5 是因为我喜欢这个数字,而且如果你放 0 就很难调整窗口的大小。

使用此短代码后,结果如下:

解决方案

ResizeMode="NoResize"现在,白色边框在没有使用and的情况下消失了AllowsTransparency="True",并且在窗口中显示了阴影。

稍后我将解释如何使用简单而简短的代码轻松地制作按钮(我没有使用图像作为按钮),我是新手,我认为我可以发布到 codeproject,因为在这里我没有找到这个地方发布教程。

也许还有另一种解决方案(我知道对于像我这样的菜鸟来说有困难和困难的解决方案)但这适用于我的个人项目。

这是完整的代码

<Window x:Class="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:Concursos"
    mc:Ignorable="d"
    Title="Concuros" Height="350" Width="525"
    WindowStyle="None"
    WindowState="Normal" 
    ResizeMode="CanResize"
    >
<WindowChrome.WindowChrome>
    <WindowChrome 
        CaptionHeight="0"
        ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

    <Grid>

    <Rectangle Fill="#D53736" HorizontalAlignment="Stretch" Height="35" VerticalAlignment="Top" PreviewMouseDown="Rectangle_PreviewMouseDown" />
    <Button x:Name="Btnclose" Content="r" HorizontalAlignment="Right" VerticalAlignment="Top" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
    <Button x:Name="Btnmax" Content="2" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,35,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
    <Button x:Name="Btnmin" Content="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,70,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>

</Grid>

谢谢!

于 2016-03-28T08:50:19.327 回答
41

虽然公认的答案非常正确,但只想指出 AllowTransparency 有一些缺点。它不允许显示子窗口控件,即 WebBrowser,并且它通常会强制软件渲染,这可能会对性能产生负面影响。

不过有更好的解决方法。

当您想要创建一个没有边框且可调整大小并且能够承载 WebBrowser 控件或指向您根本无法访问的 URL 的 Frame 控件的窗口时,所述控件的内容将显示为空。

不过,我找到了解决方法;在窗口中,如果将 WindowStyle 设置为 None,将 ResizeMode 设置为 NoResize(请耐心等待,一旦完成,您仍然可以调整大小)然后确保您有 UNCHECKED AllowsTransparency 您将有一个没有边框的静态大小的窗口并显示浏览器控件。

现在,您可能仍然希望能够调整大小,对吧?好吧,我们可以通过互操作调用来做到这一点:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    //Attach this to the MouseDown event of your drag control to move the window in place of the title bar
    private void WindowDrag(object sender, MouseButtonEventArgs e) // MouseDown
    {
        ReleaseCapture();
        SendMessage(new WindowInteropHelper(this).Handle,
            0xA1, (IntPtr)0x2, (IntPtr)0);
    }

    //Attach this to the PreviewMousLeftButtonDown event of the grip control in the lower right corner of the form to resize the window
    private void WindowResize(object sender, MouseButtonEventArgs e) //PreviewMousLeftButtonDown
    {
        HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        SendMessage(hwndSource.Handle, 0x112, (IntPtr)61448, IntPtr.Zero);
    }

瞧,一个没有边框的 WPF 窗口,仍然可以移动和调整大小,而不会失去与 WebBrowser 等控件的兼容性

于 2014-08-02T12:25:55.050 回答
5

样品在这里:

<Style TargetType="Window" x:Key="DialogWindow">
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="Black" BorderThickness="3" CornerRadius="10" Height="{TemplateBinding Height}"
                            Width="{TemplateBinding Width}" Background="Gray">
                        <DockPanel>
                            <Grid DockPanel.Dock="Top">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition Width="50"/>
                                </Grid.ColumnDefinitions>
                                <Label Height="35" Grid.ColumnSpan="2"
                                       x:Name="PART_WindowHeader"                                            
                                       HorizontalAlignment="Stretch" 
                                       VerticalAlignment="Stretch"/>
                                <Button Width="15" Height="15" Content="x" Grid.Column="1" x:Name="PART_CloseButton"/>
                            </Grid>
                            <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                        Background="LightBlue" CornerRadius="0,0,10,10" 
                                        Grid.ColumnSpan="2"
                                        Grid.RowSpan="2">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="20"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="20"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <ResizeGrip Width="10" Height="10" Grid.Column="1" VerticalAlignment="Bottom" Grid.Row="1"/>
                                </Grid>
                            </Border>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
于 2011-11-26T14:02:58.457 回答
1

我很难得到@fernando-aguirreWindowChrome用来工作的答案。在我的情况下它不起作用,因为我正在覆盖OnSourceInitializedMainWindow不是调用基类方法。

protected override void OnSourceInitialized(EventArgs e)
{
    ViewModel.Initialize(this);
    base.OnSourceInitialized(e); // <== Need to call this!
}

这让我难过了很长时间。

于 2020-03-09T15:42:25.897 回答