Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在窗口的标题区域中创建按钮?_C#_.net_Wpf_Windows - Fatal编程技术网

C# 如何在窗口的标题区域中创建按钮?

C# 如何在窗口的标题区域中创建按钮?,c#,.net,wpf,windows,C#,.net,Wpf,Windows,如何在窗口标题区的WPF中创建一个菜单按钮或一个简单按钮,如下所示: 我喜欢一个简单的解决方案。有吗?我发现这非常复杂。您需要创建自定义窗口并为其编写样式。 例如: public class CustomWindow : Window { protected void MinimizeClick(object sender, RoutedEventArgs e) { WindowState = WindowState.Minimized; }

如何在窗口标题区的WPF中创建一个菜单按钮或一个简单按钮,如下所示:


我喜欢一个简单的解决方案。有吗?我发现这非常复杂。

您需要创建自定义窗口并为其编写样式。 例如:

public class CustomWindow : Window
{
    protected void MinimizeClick(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Minimized;
    }

    protected void RestoreClick(object sender, RoutedEventArgs e)
    {
        WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
    }

    protected void CloseClick(object sender, RoutedEventArgs e)
    {
        Close();
    }
}
风格:

 <Style TargetType="{x:Type local:CustomWindow}">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="Silver"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomWindow}">
 <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Rectangle x:Name="moveRectangle" Fill="Transparent"
                                           Grid.Row="0" Grid.Column="0"/>

                                    Add custom button here

                                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                                    <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="0" />
                                    <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="1" />
                                    <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="r" />
                                </StackPanel>
                                <Grid Background="{TemplateBinding Background}"
                                           Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">
                                    <AdornerDecorator>
                                        <ContentPresenter/>
                                    </AdornerDecorator>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

您需要创建自定义窗口并为其编写样式。 例如:

public class CustomWindow : Window
{
    protected void MinimizeClick(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Minimized;
    }

    protected void RestoreClick(object sender, RoutedEventArgs e)
    {
        WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
    }

    protected void CloseClick(object sender, RoutedEventArgs e)
    {
        Close();
    }
}
风格:

 <Style TargetType="{x:Type local:CustomWindow}">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="Silver"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomWindow}">
 <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Rectangle x:Name="moveRectangle" Fill="Transparent"
                                           Grid.Row="0" Grid.Column="0"/>

                                    Add custom button here

                                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                                    <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="0" />
                                    <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="1" />
                                    <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="r" />
                                </StackPanel>
                                <Grid Background="{TemplateBinding Background}"
                                           Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">
                                    <AdornerDecorator>
                                        <ContentPresenter/>
                                    </AdornerDecorator>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

你说的是修改非客户区。我对WPF几乎没有经验,但我在谷歌上得到的第一个链接是

此Microsoft库允许您根据自己的需要自定义窗口浏览器:),而且由于您不仅仅是将整个窗口扔掉(就像使用
WindowStyle=None
时那样),整个窗口仍然按照您习惯的方式工作,包括系统按钮、拖动等


也就是说,我不认为你的样品对用户来说是件好事。您不应该干扰chrome的系统部件。左上角的菜单是窗口系统菜单,删除(或替换)它不是很友好。如果您决定无论如何都要更换它,至少要确保它能正常工作,除了您要添加的其他部件。

您说的是修改非客户端区域。我对WPF几乎没有经验,但我在谷歌上得到的第一个链接是

此Microsoft库允许您根据自己的需要自定义窗口浏览器:),而且由于您不仅仅是将整个窗口扔掉(就像使用
WindowStyle=None
时那样),整个窗口仍然按照您习惯的方式工作,包括系统按钮、拖动等


也就是说,我不认为你的样品对用户来说是件好事。您不应该干扰chrome的系统部件。左上角的菜单是窗口系统菜单,删除(或替换)它不是很友好。如果您决定替换它,至少要确保除了要添加的附加部分外,它能正常工作。

另一个方法是通过全屏模式启动应用程序来隐藏应用程序的标题栏。(设置WindowState=“最大化”WindowStyle=“无”)


然后,您可以像以前一样将菜单按钮放在客户端区域的最顶部。

另一种方法是,只需在全屏模式下启动应用程序,即可隐藏其标题栏。(设置WindowState=“最大化”WindowStyle=“无”)

然后,您可以像之前一样将菜单按钮放置在客户端区域的最顶部