使用Timer进行Delphi表格切换

时间:2012-07-28 07:50:02

标签: delphi delphi-xe2

我是德尔福学习者。 A有一个项目有2个表格(“Form001”和“Form002”)。 “Form001”上有一个Button(“BitBtn001”)。 “Form001”和“Form002”都由“FadeInTimer”创建,并使用“FadeOutTimer”的“FormCloseQuery”关闭。两个表单都被“FormMinimizeTimer”最小化,并由“FormRetoreTimer”恢复。如果我点击“BitBtn001”,则显示“Form002”。

Here is the Download Link of my Project :

以下是实施逻辑:

表格001:

unit KoushikHalder001;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  Vcl.Imaging.pngimage;

type
  TForm001 = class(TForm)
    Edit001: TEdit;
    Background: TImage;
    BitBtn001: TBitBtn;
    BitBtn002: TBitBtn;
    FadeInTimer: TTimer;
    FadeOutTimer: TTimer;
    FormMinimizeTimer: TTimer;
    FormRestoreTimer: TTimer;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn001Click(Sender: TObject);
    procedure BitBtn002Click(Sender: TObject);
    procedure FadeInTimerTimer(Sender: TObject);
    procedure FadeOutTimerTimer(Sender: TObject);
    procedure FormClosingTimerTimer(Sender: TObject);
    procedure FormMinimizeTimerTimer(Sender: TObject);
    procedure FormRestoreTimerTimer(Sender: TObject);
  private
    { Private declarations }
    CrossButtonClick: Boolean;
    procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest;
    procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SysCommand;
  public
    { Public declarations }
  end;

var
  Form001: TForm001;

implementation

{$R *.dfm}

uses KoushikHalder002;

procedure TForm001.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  if ControlAtPos(ScreenToClient(Msg.Pos), True, True, True)= nil
    then
      begin
        if Msg.Result=htClient then Msg.Result := htCaption;
      end;
end;

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
  if Form002.WindowState = wsMinimized then
    begin
      Form002.FormRestoreTimer.Enabled := true;
    end
    else
    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.FormHide(Sender: TObject);
begin
  FadeOutTimer.Enabled := true;
end;

procedure TForm001.FormShow(Sender: TObject);
begin
  FadeInTimer.Enabled := true;
end;

procedure TForm001.FadeInTimerTimer(Sender: TObject);
begin
  if Form001.AlphaBlendValue >= 220
    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.FormClosingTimerTimer(Sender: TObject);
begin
  DoubleBuffered := true;
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.

表格002:

unit KoushikHalder002;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  Vcl.Imaging.pngimage;

type
  TForm002 = class(TForm)
    Edit001: TEdit;
    Background: TImage;
    BitBtn001: TBitBtn;
    BitBtn002: TBitBtn;
    FadeInTimer: TTimer;
    FadeOutTimer: TTimer;
    FormClosingTimer: TTimer;
    FormMinimizeTimer: TTimer;
    FormRestoreTimer: TTimer;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormHide(Sender: TObject);
    procedure BitBtn001Click(Sender: TObject);
    procedure BitBtn002Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FadeInTimerTimer(Sender: TObject);
    procedure FadeOutTimerTimer(Sender: TObject);
    procedure FormClosingTimerTimer(Sender: TObject);
    procedure FormMinimizeTimerTimer(Sender: TObject);
    procedure FormRestoreTimerTimer(Sender: TObject);
  private
    { Private declarations }
    CrossButtonClick: Boolean;
    procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest;
    procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SysCommand;
  public
    { Public declarations }
  end;

var
  Form002: TForm002;

implementation

{$R *.dfm}

uses KoushikHalder001;

procedure TForm002.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  if ControlAtPos(ScreenToClient(Msg.Pos), True, True, True)= nil
    then
      begin
        if Msg.Result=htClient then Msg.Result := htCaption;
      end;
end;

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 >= 220
    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.FormClosingTimerTimer(Sender: TObject);
begin
  if Form002.AlphaBlendValue <= 0
    then
      begin
        FadeOutTimer.Enabled := false;
        CrossButtonClick := false;
        Form001.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.

这里“CrossButtonClick”是“FormClosingQuery”中使用的全局布尔变量。

除了两个问题外,每件事都很好。

第01期: 如果我使用标题栏上的“X”按钮关闭“Form002”并再次单击(如“Image001”所示)Image001 在“BitBtn001”上再次显示“Form002”。但问题是,如果“Form002”最小化并由“BitBtn001”显示,然后通过单击“X”按钮关闭它(如“Image002”所示)Image002 并再次按“BitBtn001”显示“Form002”,但遗憾的是“Form002”未再显示。

第02期: 如果我使用标题中的“RestoreUp”按钮恢复“Form002”(如“Image003”所示),“FormRestoreTimer”没有做任何事情。Image003它通常会被恢复。请帮我。请告诉我我的错误。

1 个答案:

答案 0 :(得分:1)

错误是:

procedure TForm001.BitBtn001Click(Sender: TObject);
begin
  if Form002.WindowState = wsMinimized then
    begin
      Form002.Perform(WM_SYSCOMMAND, SC_RESTORE, 0);  // ! - missed in your code
//      Form002.FormRestoreTimer.Enabled := true;   - not needed anymore
    end
    else
    Form002.show;
end;

恢复Form002时可能看不到褪色效果,因为褪色速度太快。