Posts Tagged ‘delphi form events’

This demo will demonstrate how to use the TgtFormEvents.
gtformeventsdemo_screen

Here is the source code of the project

procedure TForm1.Button1Click(Sender: TObject);
var
  AFrm : TForm;
begin
  //Creating a Form
  AFrm                     := TForm.Create(Self);
  AFrm.Position            := poScreenCenter;
  //Creating the FormEvents component
  FFormEvents              := TgtFormEvents.Create(Self);
  //Attaching the FormEvents component to the form
  FFormEvents.Form         := AFrm;

  //Assiging the events
  FFormEvents.OnBeforeShow := OnBeforeShow;
  FFormEvents.OnAfterShow  := OnAfterShow;
  FFormEvents.OnMinimize   := OnMinimize;
  FFormEvents.OnRestore    := OnRestore;

  try
    //Showing the form
    if CheckBox1.Checked then
      AFrm.ShowModal
    else
      AFrm.Show;
  finally
    //Destroying the form
    if CheckBox1.Checked  then
    begin
      FreeAndNil(AFrm);
      FreeAndNil(FFormEvents);
    end;
  end;
end;

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Download GT FormEvents Demo

The classic TForm does not expose or does not have some usefull events like OnMinimize,OnRestore,OnBeforeShow,OnAfterShow.

The OnShow event of the Form occurs before the form is shown and that’s ok if the form is not modal but if it is modal is there
a way to get an event after the form is shown even if the form is modal ?

Well the classic TForm does not provide this kind of information and the TForm is pretty messy to make a descentant and install it into the IDE
so i’ve turned into implementing this at runtime.

TgtFormEvents is attached to a TForm or a descentant of TForm and implements 4 events
OnBeforeShow
OnAfterShow (even if the form is modal!)
OnMinimize
OnRestore

Here is the source code :

{*******************************************************}
{                                                       }
{       GT Delphi Components                            }
{       TgtFormEvents                                   }
{                                                       }
{       Copyright (c) GT Delphi Components              }
{       http://www.gtdelphicomponents.gr                }
{                                                       }
{                                                       }
{*******************************************************}

unit o_FormEvents;

interface
uses
   Classes
  ,Forms
  ,Messages
  ;
type
{------------------------------------------------------------------------------}
  TgtFormEvents = class(TComponent)
  private
    FForm          : TForm;
    FOnAfterShow   : TNotifyEvent;
    FOnMinimize    : TNotifyEvent;
    FOnBeforeShow  : TNotifyEvent;
    FOnRestore     : TNotifyEvent;
    procedure SetForm(const Value: TForm);
    { Private declarations }
  protected
    { Protected declarations }
    FOldFormWndProc : TWndMethod;
    procedure Notification(AComponent: TComponent;Operation: TOperation);override;
    procedure NewWndProc(var Message : TMessage);
  public
    { Public declarations }
    constructor Create(AOwner:TComponent);override;
    destructor  Destroy;override;
  published
    { Published declarations}
    property Form : TForm read FForm write SetForm;
  published
    property OnBeforeShow : TNotifyEvent read FOnBeforeShow write FOnBeforeShow;
    property OnAfterShow  : TNotifyEvent read FOnAfterShow  write FOnAfterShow;
    property OnMinimize   : TNotifyEvent read FOnMinimize   write FOnMinimize;
    property OnRestore    : TNotifyEvent read FOnRestore    write FOnRestore;
  end;
{------------------------------------------------------------------------------}

implementation
uses
  Dialogs
  ,Windows
  ;
const
  WM_AFTER_SHOW = WM_USER + 1001;

{ TgtFormEvents }
{------------------------------------------------------------------------------}
constructor TgtFormEvents.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;
{------------------------------------------------------------------------------}
destructor TgtFormEvents.Destroy;
begin
  inherited;
end;
{------------------------------------------------------------------------------}
procedure TgtFormEvents.Notification(AComponent: TComponent;Operation: TOperation);
begin
  if Operation = opRemove then
    if AComponent = FForm then
      Form := nil;
  inherited Notification(AComponent,Operation);
end;
{------------------------------------------------------------------------------}
procedure TgtFormEvents.NewWndProc(var Message: TMessage);
begin
  case Message.Msg of
    WM_SHOWWINDOW :
      begin
        case Message.WParam of
          0 : ;
          1 :
            begin
              if Assigned(FOnBeforeShow) then
                FOnBeforeShow(FForm);
              PostMessage(FForm.Handle,WM_AFTER_SHOW,0,0);
            end
        end;
      end;
    WM_AFTER_SHOW :
      begin
        if Assigned(FOnAfterShow) then
          FOnAfterShow(FForm);
      end;
    WM_SYSCOMMAND :
      begin
        case Message.WParam of
          SC_MINIMIZE :
            begin
              if Assigned(FOnMinimize) then
                FOnMinimize(FForm);
            end;
          SC_RESTORE  :
            begin
              if Assigned(FOnRestore) then
                FOnRestore(FForm);
            end;
        end;
     end;
  end;
  FOldFormWndProc(Message);
end;
{------------------------------------------------------------------------------}

{------------------------------------------------------------------------------}
procedure TgtFormEvents.SetForm(const Value: TForm);
begin
  if Assigned(FForm) then
  begin
    FForm.RemoveFreeNotification(Self);
    FForm.WindowProc := FOldFormWndProc;
    //Unhooking the forms WndProc
  end;

  FForm := Value;

  if Assigned(FForm) then
  begin
    FForm.FreeNotification(Self);
    FOldFormWndProc  := FForm.WindowProc;
    //Keeping the original Forms WndProc
    FForm.WindowProc := NewWndProc;
    //Hooking the forms WndProc
  end;
end;
{------------------------------------------------------------------------------}
end.
A support Vote
  • Top Borland Sites
Site Information
  • http://www.gtdelphicomponents.gr/?feed=rss2
Sponsors
  • http://www.rfsource.gr
Login

UserOnline