0
votes

I have a very generic WPF window that starts maximized. When this window is minimized to task bar, and then restored the window no longer fills the screen. It shows a small portion of the desktop on the right hand side. Windows still considers it as maximized and when set to normal and then maximized again it looks like it should until being minimized again.

    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; }
    }
}

Your thoughts? UPDATE:

Seems to related to the can resize property. If I set it to ca minimize it is fine. resize with grip or can resize cause the window to change sizes after being minimized to task bar.

<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>

UPDATE 2: screenshot: https://ibb.co/iN2n7F Screenshot of Answer: https://ibb.co/c3BHra

ANSWER: Issue is something with my display only. Haven't figured it out yet, but the program operates as expected on another PC. Many thanks to @Vanna who helped me parse that out.

1
I edited my post. The MaxBtn and ResBtn were leftovers from trying other things. setting the max height and width was a fix so the WPF window did not hide the task bar. Using your code, the window still hides the task bar.Phexyaa

1 Answers

1
votes

Edit
This code is tested working

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;
}