I found the solution by getting the initial value of
ScaleX
and
ScaleY
.
This is the perfect solution. By "
perfect," I mean that this solution works properly even if the user clicks on the application icon in the taskbar.
IntPtr
should be used in this specific case instead of the
StateChanged
event to handle the
Minimize event.
<Window x:Name="GanjAsemanMainWindow" x:Class="Ganj_Aseman.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"
mc:Ignorable="d"
AllowsTransparency="True" Background="Transparent" FontSize="13" Height="535" RenderTransformOrigin="0.5,0.5" ResizeMode="CanResizeWithGrip" Title="MainWindow" Width="764" WindowStyle="None">
<Window.RenderTransform>
<TransformGroup>
<RotateTransform/>
<ScaleTransform/>
</TransformGroup>
</Window.RenderTransform>
<Window.Resources>
<Storyboard x:Key="MinimizeMainWindow" Storyboard.TargetName="GanjAsemanMainWindow" Completed="MinimizeMainWindow_Completed">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].Angle" Duration="0:0:2" To="360"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:2" To="0"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:2" To="0"/>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2.5" To="0"/>
</Storyboard>
<Storyboard x:Key="NormalizeMainWindow" Storyboard.TargetName="GanjAsemanMainWindow" Completed="NormalizeMainWindow_Completed">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].Angle" Duration="0:0:2" To="0"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:2" To="1"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:2" To="1"/>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2.5" To="100"/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle x:Name="TitleRectangle" Fill="#727A7A7A" Stroke="WhiteSmoke" StrokeThickness="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" RadiusX="10" RadiusY="10" RenderTransformOrigin="0.5,0.5">
<Rectangle.Effect>
<BlurEffect Radius="2"/>
</Rectangle.Effect>
</Rectangle>
<Rectangle x:Name="MainRectangle" Fill="#d4fed8" RadiusY="5" RadiusX="5" Stroke="Gray" Margin="7,50,7,7" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Image x:Name="CloseIconImage" PreviewMouseLeftButtonDown="CloseIconImage_PreviewMouseLeftButtonDown" Focusable="True" FocusVisualStyle="{x:Null}" Cursor="Hand" HorizontalAlignment="Right" Height="47" VerticalAlignment="Top" Source="Images/CloseIcon.png" Width="52" Margin="0,2,11,0"/>
<Image x:Name="MinimizeIconImage" Focusable="True" FocusVisualStyle="{x:Null}" Cursor="Hand" Height="47" VerticalAlignment="Top" Source="Images/MinimizeIcon.png" Margin="0,2,67,0" HorizontalAlignment="Right" Width="52">
<Image.Triggers>
<EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
<BeginStoryboard Storyboard="{StaticResource MinimizeMainWindow}">
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Animation;
namespace Ganj_Aseman
{
public partial class MainWindow : Window
{
bool Normalization = true;
private void OnSourceInitialized(object sender, EventArgs e)
{
HwndSource Source = (HwndSource)PresentationSource.FromVisual(this);
Source.AddHook(new HwndSourceHook(Minimize));
Source.AddHook(new HwndSourceHook(Normalize));
}
public MainWindow()
{
InitializeComponent();
SourceInitialized += new EventHandler(OnSourceInitialized);
}
private IntPtr Minimize(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF020)
{
handled = true;
if (WindowState == WindowState.Normal && Normalization == true)
{
(Resources["MinimizeMainWindow"] as Storyboard).Begin();
}
}
return IntPtr.Zero;
}
private IntPtr Normalize(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF120 && WindowState == WindowState.Minimized)
{
(Resources["NormalizeMainWindow"] as Storyboard).Begin();
}
return IntPtr.Zero;
}
private void MinimizeMainWindow_Completed(object sender, EventArgs e)
{
WindowState = WindowState.Minimized;
Normalization = false;
}
private void NormalizeMainWindow_Completed(object sender, EventArgs e)
{
WindowState = WindowState.Normal;
Normalization = true;
}
private void CloseIconImage_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Close();
}
}
}
Tested in:
• .NET Framework 4.5
• WPF
Thank you for your time.
Best regards,
Reza Jaferi