我正在WPF中写一个模态对话框。我如何设置一个WPF窗口没有关闭按钮?我仍然希望它的WindowState有一个正常的标题栏。

我找到了ResizeMode、WindowState和WindowStyle,但这些属性都不允许我隐藏关闭按钮,而是显示标题栏,就像在模态对话框中一样。


当前回答

这就是你的问题所在。窗口框右上方的关闭按钮不是WPF窗口的一部分,但它属于由操作系统控制的窗口框的一部分。这意味着你必须使用Win32互操作来实现。

或者,您可以使用noframe并提供您自己的“框架”或根本没有框架。

其他回答

这不会消除关闭按钮,但它将阻止某人关闭窗口。

把这个放在文件后面的代码中:

protected override void OnClosing(CancelEventArgs e)
{
   base.OnClosing(e);
   e.Cancel = true;
}

这就是你的问题所在。窗口框右上方的关闭按钮不是WPF窗口的一部分,但它属于由操作系统控制的窗口框的一部分。这意味着你必须使用Win32互操作来实现。

或者,您可以使用noframe并提供您自己的“框架”或根本没有框架。

WPF没有一个内置的属性来隐藏标题栏的关闭按钮,但是你可以用几行P/Invoke来做到这一点。

首先,将这些声明添加到Window类中:

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

然后把下面的代码放到Window的Loaded事件中:

var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

这样就没有关闭按钮了。你也不会在标题栏的左边有一个窗口图标,这意味着没有系统菜单,即使你右键单击标题栏——它们都在一起。

重要提示:所有这些都是隐藏按钮。用户仍然可以关闭窗口!如果用户按下Alt+F4,或者通过任务栏关闭应用程序,窗口仍然会关闭。

如果你不想让窗口在后台线程完成之前关闭,那么你也可以覆盖OnClosing并将Cancel设置为true,就像Gabe建议的那样。

使用WindowStyle="SingleBorderWindow",这将隐藏WPF窗口的最大和最小按钮。

下面是我如何实现类似的目标使用自定义样式没有DllImports和P/Invoke调用。这删除现有的标题栏使用WindowStyle="none",并显示一个'TextBlock'与类似的背景颜色表明作为标题栏。

XAML 代码

<Window x:Class="AddBook"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="http://wpftoolkit.my-libraries.com/v5" 
    WindowStartupLocation="CenterOwner"        
    ResizeMode="NoResize" 
    Style="{DynamicResource WindowStyleX}"
    ShowInTaskbar="False"
    ShowActivated="True"
    SizeToContent="Height"
    Title="Add New Book" 
    Width="450">
..............

</Window>

XAML

<Style x:Key="WindowStyleX" TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None" />
<Setter Property="AllowsTransparency" Value="False" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="Background" Value="White" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Window}">
            <Border BorderBrush="{DynamicResource BlackColor}" BorderThickness="1">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Border
                        Grid.Row="0"
                        Grid.ColumnSpan="2"
                        Background="{DynamicResource BlackColor}">
                        <Grid>
                            <TextBlock
                                Grid.Column="1"
                                Margin="10,0,0,0"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                FontSize="16"
                                Foreground="{DynamicResource WhiteTextForeground}"
                                Text="{TemplateBinding Title}" />
                        </Grid>
                    </Border>
                    <ContentPresenter Grid.Row="1" />
                </Grid>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>