Effect of fading Windows window to (Minimize, Maximize and Close)

Asked

Viewed 119 times

0

I left the WindowStyle="None" and that eliminates all the animation of the window.

A normal window edged has the excitement of fading to the (minimize, maximize and close).

I wish that same effect existed in a windowless.

That’s the XAML with ready interface.

<Window x:Class="my_project_test.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"
        AllowsTransparency="True"
        WindowStyle="None"
        Height="700" 
        Width="1080"
        xmlns:local="clr-namespace:my_project_test"
        mc:Ignorable="d"
        Title="MainWindow">
    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Column="0" Grid.ColumnSpan="4" Background="Black" MouseLeftButtonDown="DragMove_Click"></StackPanel>
            <Button Grid.Column="1" Margin="2" Content="minimize" Click="ButtonMinimize_Click"></Button>
            <Button Grid.Column="2" Margin="2" Content="maximize" Click="ButtonMaximize_Click"></Button>
            <Button Grid.Column="3" Margin="2" Content="close" Click="ButtonClose_Click"></Button>
        </Grid>
    </Grid>
</Window>

Complete code:

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


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

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

    private void ButtonClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void DragMove_Click(object sender, MouseButtonEventArgs e)
    {
        this.DragMove();
    }
}

As you can see, the window has no animation when clicking on any of the 3 button above.

How can I cheer up that window to (maximize, minimize and close), using the 3 buttons above?

1 answer

0


I got a solution to this problem.

As a window without edge doesn’t have the same animations as a window with edge, so I need to leave on XAML:

WindowStyle="SingleBorderWindow"
ResizeMode="CanResize"

And I added this line that will eliminate the edges:

<WindowChrome.WindowChrome>
    <WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0" UseAeroCaptionButtons="False" ResizeBorderThickness="7"/>
</WindowChrome.WindowChrome>

The end result is this:

XAML

<Window x:Class="my_project_test.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"
        WindowStyle="SingleBorderWindow"
        ResizeMode="CanResize"
        Height="700" 
        Width="1080"
        xmlns:local="clr-namespace:my_project_test"
        mc:Ignorable="d"
        x:Name="winHelp"
        Title="MainWindow">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0" UseAeroCaptionButtons="False" ResizeBorderThickness="7"/>
    </WindowChrome.WindowChrome>
    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Column="0" Grid.ColumnSpan="4" Background="Black" 
                        MouseLeftButtonDown="Header_MouseLeftButtonDown"
                        MouseLeftButtonUp="Header_MouseLeftButtonUp"
                        MouseMove="Header_MouseMove"
                        ></StackPanel>
            <Button Grid.Column="1" Margin="2" Content="minimize" Click="ButtonMinimize_Click"></Button>
            <Button Grid.Column="2" Margin="2" Content="maximize" Click="ButtonMaximize_Click"></Button>
            <Button Grid.Column="3" Margin="2" Content="close" Click="ButtonClose_Click"></Button>
        </Grid>
    </Grid>
</Window>

Code:

public partial class MainWindow : Window
{
    private bool mRestoreIfMove = false;

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


    #region region_events_window

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

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

    private void ButtonClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void SwitchWindowState()
    {
        switch (WindowState)
        {
            case WindowState.Normal:
                {
                    WindowState = WindowState.Maximized;
                    break;
                }
            case WindowState.Maximized:
                {
                    WindowState = WindowState.Normal;
                    break;
                }
        }
    }

    private void Header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2)
        {
            if ((ResizeMode == ResizeMode.CanResize) || (ResizeMode == ResizeMode.CanResizeWithGrip))
            {
                SwitchWindowState();
            }

            return;
        }

        else if (WindowState == WindowState.Maximized)
        {
            mRestoreIfMove = true;
            return;
        }

        DragMove();
    }

    private void Header_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        mRestoreIfMove = false;
    }

    private void Header_MouseMove(object sender, MouseEventArgs e)
    {
        if (mRestoreIfMove)
        {
            mRestoreIfMove = false;

            double percentHorizontal = e.GetPosition(this).X / ActualWidth;
            double targetHorizontal = RestoreBounds.Width * percentHorizontal;

            double percentVertical = e.GetPosition(this).Y / ActualHeight;
            double targetVertical = RestoreBounds.Height * percentVertical;

            WindowState = WindowState.Normal;

            POINT lMousePosition;
            GetCursorPos(out lMousePosition);

            Left = lMousePosition.X - targetHorizontal;
            Top = lMousePosition.Y - targetVertical;

            DragMove();
        }
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out POINT lpPoint);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }
    #endregion
}

Done all this, the window without ledge is behaving like a normal window with its proper animations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.