(*  TMessageTracer v. 1.06
    Copyright (c) 1998-2003 y CoolDev.Com.
    Delphi 3/4/5/6/7
    C++Builder 3/4/5/6/7

    Although TMessageTracer is being distributed as Freeware with full
    source codes included, IT IS part of CoolControls package and
    should be delivered un-modified ONLY togeather with CoolControls.
*)

{$I versions.inc}
unit VclTracer;

interface

uses
  Windows, Messages, Classes, Controls, CoolCtrls, SysUtils, Forms;

type
  // TMessageTracer is the system that monitors Windows messages sent to the
  // particular windowed control. It has the shell that allows to set up which messages
  // will be monitored by the component. Works in both run and design time.
  TMessageTracer = class(TComponent)
   private
     FControl: TWinControl;
     FAlwaysOnTop: Boolean;
     FAutoLoad: Boolean;
     FAboutBox: TCCAboutBox;
     procedure SetControl(Value: TWinControl);
     procedure SetAlwaysOnTop(Value: Boolean);
     function GetAlwaysOnTop: Boolean;
   protected
     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
     procedure Loaded; override;
   public
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
     // Executes the shell.
     procedure Execute; virtual;
   published
    // Displays information about the control and package version.
     property About: TCCAboutBox read FAboutBox write FAboutBox;
     // Set to True if you want the form to be top most window.
     property AlwaysOnTop: Boolean read GetAlwaysOnTop write SetAlwaysOnTop default False;
     // When True the shell will be activated automatically on the form's start up.
     property AutoLoad: Boolean read FAutoLoad write FAutoLoad default False;
     // Control to be monitored for messages.
     property Control: TWinControl read FControl write SetControl;
   end;

implementation

uses
  MsgTracer;

{TMessageTracer}
constructor TMessageTracer.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  TracerForm := TTracerForm.Create(Application);

  FAlwaysOnTop := False;
  FAutoLoad := False;
  FControl := nil;
end;

destructor TMessageTracer.Destroy;
begin
  if Assigned(TracerForm) then
    TracerForm.Free;

  inherited Destroy;
end;

procedure TMessageTracer.SetControl(Value: TWinControl);
begin
  FControl := Value;
  if Assigned(TracerForm) and Assigned(TracerForm.WndHook) then
    TracerForm.WndHook.WinControl := Value;
end;

function TMessageTracer.GetAlwaysOnTop: Boolean;
begin
  if Assigned(TracerForm) then
    Result := TracerForm.TopMost else
      Result := False;
end;

procedure TMessageTracer.SetAlwaysOnTop(Value: Boolean);
begin
  FAlwaysOnTop := Value;

  if Assigned(TracerForm) then
    TracerForm.TopMost := Value;
end;

procedure TMessageTracer.Execute;
begin
  if Assigned(TracerForm) then
    TracerForm.Show;
end;

procedure TMessageTracer.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent is TWinControl) and (AComponent = FControl) then
    Control := nil;
end;

procedure TMessageTracer.Loaded;
begin
  inherited Loaded;
  if not (csDesigning in ComponentState) then
    if FAutoLoad then
      Execute else else
        TracerForm.Hide;
end;

end.