Delphi Form Switching using Timer
===

I am a Delphi learner. A have a Project having 2 Forms("Form001" and "Form002"). There is a Button("BitBtn001") on "Form001". Both "Form001" and "Form002" is created by "FadeInTimer" and closed using "FormCloseQuery" by "FadeOutTimer". Both the Forms are minimized by "FormMinimizeTimer" and restored by "FormRetoreTimer". If I click on "BitBtn001" then "Form002" is shown. The following is implementation logic :

**Form001 :**

      private
        CrossButtonClick: Boolean;
        procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SysCommand;
      public
        { Public declarations }
      end;
    
    var
      Form001: TForm001;
    
    implementation
    
    {$R *.dfm}
    
    uses KoushikHalder002;
    
    procedure TForm001.WMSysCommand(var Msg: TWMSysCommand);
    begin
      case Msg.CmdType of
        SC_MINIMIZE:
          begin
            if Form001.AlphaBlendValue > 0 then
              begin
                FormMinimizeTimer.Enabled := true;
                Exit;
              end;
          end;
        SC_RESTORE:
          begin
            if Form001.AlphaBlendValue < 220 then
              begin
                FormRestoreTimer.Enabled := True;
              end;
          end;
      end;
      inherited;
    end;
    
    procedure TForm001.BitBtn001Click(Sender: TObject);
    begin
      Form002.show
    end;

    procedure TForm001.BitBtn002Click(Sender: TObject);
    begin
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm001.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm001.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      if CrossButtonClick = true
        then
          begin
            CanClose := true;
            Exit;
          end;
      CanClose := false;
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm001.FormCreate(Sender: TObject);
    begin
      FadeInTimer.Enabled := true;
    end;
    
    procedure TForm001.FormShow(Sender: TObject);
    begin
      FadeInTimer.Enabled := true;
    end;
    
    procedure TForm001.FadeInTimerTimer(Sender: TObject);
    begin
      if Form001.AlphaBlendValue >= 200
        then
          begin
            FadeInTimer.Enabled := false;
          end
        else
          begin
            Form001.AlphaBlendValue := Form001.AlphaBlendValue + 10;
            CrossButtonClick := false;
          end;
    end;
    
    procedure TForm001.FadeOutTimerTimer(Sender: TObject);
    begin
      if Form001.AlphaBlendValue <= 0
        then
          begin
            FadeOutTimer.Enabled := false;
            CrossButtonClick := true;
            Self.Close;
          end
        else
          begin
            Form001.AlphaBlendValue := Form001.AlphaBlendValue - 10;
            CrossButtonClick := true;
          end;
    end;
    
    procedure TForm001.FormMinimizeTimerTimer(Sender: TObject);
    begin
      if Form001.AlphaBlendValue > 0 then
        begin
          Form001.AlphaBlendValue := Form001.AlphaBlendValue - 10;
        end
      else
        begin
          FormMinimizeTimer.Enabled := false;
          Perform(WM_SYSCOMMAND, SC_MINIMIZE, 0);
        end;
    end;
    
    procedure TForm001.FormRestoreTimerTimer(Sender: TObject);
    begin
      if Form001.AlphaBlendValue < 220 then
        begin
          Form001.AlphaBlendValue := Form001.AlphaBlendValue + 10;
        end
      else
        begin
          FormRestoreTimer.Enabled := False;
        end;
    end;
    
    end.

**Form002 :**

      private
        CrossButtonClick: Boolean;
        procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SysCommand;
    
    var
      Form002: TForm002;
    
    implementation
    
    {$R *.dfm}
    
    uses KoushikHalder001;
    
    procedure TForm002.WMSysCommand(var Msg: TWMSysCommand);
    begin
      case Msg.CmdType of
        SC_MINIMIZE:
          begin
            if Form002.AlphaBlendValue > 0 then
              begin
                FormMinimizeTimer.Enabled := true;
                Exit;
              end;
          end;
        SC_RESTORE:
          begin
            if Form002.AlphaBlendValue < 220 then
              begin
                FormRestoreTimer.Enabled := True;
              end;
          end;
      end;
      inherited;
    end;


    procedure TForm002.BitBtn001Click(Sender: TObject);
    begin
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm002.BitBtn002Click(Sender: TObject);
    begin
      FormClosingTimer.Enabled := true;
    end;
    
    procedure TForm002.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      if CrossButtonClick = true
        then
          begin
            CanClose := true;
            Exit;
          end;
      CanClose := false;
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm002.FormHide(Sender: TObject);
    begin
      FadeOutTimer.Enabled := true;
    end;
    
    procedure TForm002.FormShow(Sender: TObject);
    begin
      FadeInTimer.Enabled := true;
    end;
    
    procedure TForm002.FadeInTimerTimer(Sender: TObject);
    begin
      if Form002.AlphaBlendValue >= 200
        then
          begin
            FadeInTimer.Enabled := false;
          end
      else
        begin
          Form002.AlphaBlendValue := Form002.AlphaBlendValue + 10;
          CrossButtonClick := false;
        end;
    end;
    
    procedure TForm002.FadeOutTimerTimer(Sender: TObject);
    begin
      if Form002.AlphaBlendValue <= 0
        then
          begin
           FadeOutTimer.Enabled := false;
           CrossButtonClick := true;
           Self.Close;
          end
        else
          begin
            Form002.AlphaBlendValue := Form002.AlphaBlendValue - 10;
            CrossButtonClick := false;
          end;
    end;
      
    procedure TForm002.FormMinimizeTimerTimer(Sender: TObject);
    begin
      if Form002.AlphaBlendValue > 0 then
        begin
          Form002.AlphaBlendValue := Form002.AlphaBlendValue - 10;
        end
      else
        begin
          FormMinimizeTimer.Enabled := false;
          Perform(WM_SYSCOMMAND, SC_MINIMIZE, 0);
        end;
    end;
    
    procedure TForm002.FormRestoreTimerTimer(Sender: TObject);
    begin
      if Form002.AlphaBlendValue < 220 then
        begin
          Form002.AlphaBlendValue := Form002.AlphaBlendValue + 10;
        end
      else
        begin
          FormRestoreTimer.Enabled := False;
        end;
    end;
    
    end.

Here "CrossButtonClick" is a Global Boolean variable used in "FormClosingQuery".

Every thing works fine except two issues.

**Issue 01:**
If I close "Form002" using the "X" Button on caption bar and again click (as shown in "Image001")![Image001][1]
on "BitBtn001" then "Form002" is shown again. But the problem is that if the "Form002" is minimizing and shown by "BitBtn001" and then close it by clicking "X" button (as shown in "Image002")![Image002][2]
and again press on "BitBtn001" to show "Form002" but unfortunately "Form002" is not shown again.

**Issue 02:**
If I restore "Form002" using the "RestoreUp" button in the caption (as shown in "Image003") "FormRestoreTimer" is not doing anything.![Image003][3] It is just normally restored up. Please help me. Please tell me about my mistake.


  [1]: http://i.stack.imgur.com/FDe1t.jpg
  [2]: http://i.stack.imgur.com/enHWh.jpg
  [3]: http://i.stack.imgur.com/ZBPTn.jpg