如何制作具有默认标题栏功能的透明WPF窗口?

时间:2019-04-01 01:55:47

标签: wpf

我正在使用WPF编写一个小应用程序。我想用不透明的控件使窗口的内部透明,而标题栏(以及移动,最小化,最大化,调整大小,关闭等的功能)保持稳定。

但是由于某些原因,WPF不允许我在将AllowTransparency设置为true时使用默认标题栏,这迫使我将WindowStyle设置为None,这不是我想要的。有解决方法吗?

我的应用程序和问题没有那么先进,以至于从头开始是一个问题。

2 个答案:

答案 0 :(得分:1)

要实现窗口透明,您需要设置以下内容

WindowStartupLocation="CenterScreen"
AllowsTransparency ="True"
WindowStyle="None"
Background="Transparent"

最大化,最小化和关闭可以自己完成:

XAML:

  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Rectangle Fill="Brown" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"></Rectangle>
    <WrapPanel HorizontalAlignment="Right" VerticalAlignment="Top" Height="30" Width="Auto">
        <Button Width="20" Height="20" Margin="5" Click="Button_Click_1">_</Button>
        <Button Width="20" Height="20" Margin="5" Click="Button_Click_2">口</Button>
        <Button Width="20" Height="20" Margin="5" Click="Button_Click_3">X</Button>
    </WrapPanel>
</Grid>

代码:

    /// <summary>
    /// Min
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        if (this.WindowState != WindowState.Minimized)
        {
            this.WindowState = WindowState.Minimized;
        }
    }

    /// <summary>
    /// Max
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        if (this.WindowState != WindowState.Maximized)
        {
            this.WindowState = WindowState.Normal;
        }
    }

    /// <summary>
    /// Close
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    /// <summary>
    /// DragMove
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Rectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.DragMove();
    }

答案 1 :(得分:0)

这是最大化代码的改进版本

private void Button_Click_2(object sender, RoutedEventArgs e)
{
  if (this.WindowState != WindowState.Maximized)
  {
    this.WindowState = WindowState.Maximized;
  } else if(this.WindowState != WindowState.Normal)
  {
    this.WindowState = WindowState.Normal;
  }
}