还原后未正确显示C#WPF窗口

时间:2017-08-05 18:46:36

标签: c# wpf resize

我有一个非常通用的WPF窗口,它开始最大化。当此窗口最小化到任务栏,然后恢复窗口不再填充屏幕。它显示了右侧桌面的一小部分。 Windows仍然认为它是最大化的,当设置为正常,然后再次最大化它看起来应该再次最小化。

    public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
    }

    private void ExitBtn_Click(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }
    private void MaxBtn_Click(object sender, MouseButtonEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
        //Application.Current.MainWindow.WindowState = WindowState.Maximized;
        resizeBtn.Visibility = Visibility.Visible;
        maximizeBtn.Visibility = Visibility.Hidden;
    }
    private void ResBtn_Click(object sender, MouseButtonEventArgs e)
    {
        this.WindowState = WindowState.Normal;
        resizeBtn.Visibility = Visibility.Hidden;
        maximizeBtn.Visibility = Visibility.Visible;
    }
    private void MinBtn_Click(object sender, MouseButtonEventArgs e)
    {
        this.WindowState = WindowState.Minimized;
    }
    private void TitleBar_DblClick(object sender, MouseButtonEventArgs e)
    {
        if (this.WindowState == WindowState.Normal){ this.WindowState = WindowState.Maximized; return; }
        if (this.WindowState == WindowState.Maximized) { this.WindowState = WindowState.Normal; return; }
    }
}

你的想法? UPDATE:

似乎与can resize属性有关。如果我把它设置为ca最小化就可以了。使用grip调整大小或调整大小会导致窗口在最小化到任务栏后更改大小。

<Window x:Class="QuoteClient.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:QuoteClient"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" WindowStyle="None" Background="Black" BorderBrush="Black">
<Grid>
    <local:TitleBar VerticalAlignment="Top" MouseDoubleClick="TitleBar_DblClick"/>
    <local:ExitBtn x:Name="exitBtn" Margin="0,2.5,2.5,0" Height="15" VerticalAlignment="Top" HorizontalAlignment="Right" Width="15" MouseLeftButtonUp="ExitBtn_Click"/>
    <local:MinBtn x:Name="minimizeBtn" Margin="0,2,37,0" HorizontalAlignment="Right" Width="15" Height="15" VerticalAlignment="Top" MouseLeftButtonUp="MinBtn_Click"/>
    <local:MaxBtn x:Name="maximizeBtn" Margin="0,2,20,0" HorizontalAlignment="Right" Width="13" Height="15" VerticalAlignment="Top" MouseLeftButtonUp="MaxBtn_Click"/>
    <local:ResBtn x:Name="resizeBtn" Margin="0,2,20,0" HorizontalAlignment="Right" Width="15" Height="15" VerticalAlignment="Top"  Visibility="Hidden" MouseLeftButtonUp="ResBtn_Click"/>
    <DataGrid Margin="10,25,10,10" Background="Black" BorderBrush="{x:Null}" HorizontalGridLinesBrush="White" VerticalGridLinesBrush="White" AlternatingRowBackground="#FF575656" RowBackground="#FF403C3C" Cursor="Cross"/>
    <Expander Header="Menu" HorizontalAlignment="Left" Margin="-5,-17,0,0" Width="100" FontSize="10" RenderTransformOrigin="0.5,0.5" Height="332" VerticalAlignment="Top" BorderBrush="White" Background="Black" Foreground="#FFFFFEFE">
        <Expander.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="0.9" ScaleX="0.9"/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Expander.RenderTransform>
    </Expander>


</Grid>

更新2: 截图:https://ibb.co/iN2n7F 答案:https://ibb.co/c3BHra

的屏幕截图

答案:问题仅适用于我的显示器。尚未弄明白,但该程序在另一台PC上按预期运行。非常感谢@Vanna,他帮助我解析了这一点。

1 个答案:

答案 0 :(得分:1)

修改
此代码已经过测试

MainWindow.xml

<Window x:Class="QuoteClient.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:QuoteClient"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" WindowStyle="None" ResizeMode="CanResize" WindowStartupLocation="CenterScreen" MouseDown="Window_MouseDown" MouseDoubleClick="Window_MouseDoubleClick">

<StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Top" FlowDirection="RightToLeft">
    <Button x:Name="ExitBtn" Content="Exit" Width="75" Click="ExitApplication" />
    <Button x:Name="MaximizeBtn" Content="max" Width="75" Click="MaximizeState"/>
    <Button x:Name="NormalBtn" Content="normal" Width="75" Click="NormalState" Visibility="Collapsed"/>
    <Button x:Name="MinimizeBtn" Content="min" Width="75" Click="MinimizeState"/>
</StackPanel>
</Window>

MainWindow.cs

private static bool _isPrivStateMinimized;
private static int _left, _top;

public MainWindow()
{
    InitializeComponent();
    MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}

protected override void OnStateChanged(EventArgs e)
{            
    base.OnStateChanged(e);
    if (WindowState == WindowState.Minimized)
    {
        _isPrivStateMinimized = true;
        return;
    }
    if (_isPrivStateMinimized)
    {
        if(WindowState == WindowState.Maximized)
        {
            _left = Left;
            Left = 0;
            _top = Top;
            Top = 0;
        }
        else
        {
            Left = _left;
            Top = _top;
        }
    }
    _isPrivStateMinimized = false;
    BringIntoView();
}

private void MaximizeState(object sender, RoutedEventArgs e)
{
    WindowState = WindowState.Maximized;
    MaximizeBtn.Visibility = Visibility.Collapsed;
    NormalBtn.Visibility = Visibility.Visible;
}

private void NormalState(object sender, RoutedEventArgs e)
{
    WindowState = WindowState.Normal;
    NormalBtn.Visibility = Visibility.Collapsed;
    MaximizeBtn.Visibility = Visibility.Visible;
}

private void MinimizeState(object sender, RoutedEventArgs e)
{
    WindowState = WindowState.Minimized;
}

private void ExitApplication(object sender, RoutedEventArgs e)
{
    Close();
}

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
        DragMove();
}

private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        WindowState = WindowState.Normal;
        NormalBtn.Visibility = Visibility.Collapsed;
        MaximizeBtn.Visibility = Visibility.Visible;
        return;
    }
    WindowState = WindowState.Maximized;
    MaximizeBtn.Visibility = Visibility.Collapsed;
    NormalBtn.Visibility = Visibility.Visible;
}