Einzelnen Beitrag anzeigen

Benutzerbild von TERWI
TERWI
Online

Registriert seit: 29. Mär 2008
Ort: D-49626
381 Beiträge
 
Delphi 11 Alexandria
 
#7

AW: Fenster zur Laufzeit in einer Klasse - Fehler bei FREE

  Alt 31. Mär 2013, 19:23
Gerne, Uwe. In Auszügen mal die Logger-Klasse. Sonst wird's zu lang, der Rest funzt ja.

Code:
type
......
  TLOGWIN = class(TForm)
    lb_LOG  : TListBox;
    cb_W2Log : TCheckBox;
    cb_AOT  : TCheckBox;
    btn_Hide : TBitBtn;
    procedure btn_HideClick(Sender: TObject);
  end;
  ......
  TLOG = class
  protected
    .....
    FOnMessageLock : TBCCritSec;
    FLogList      : TStringList; // internal list of messages
    FLogTimer     : TTimer;      // internal timer to read from list
    FLOGWIN       : TLOGWIN;     // the Logger-Window
    FFileStream   : TFileStream; // write the log-file....
    .....
    procedure SetLogWin(Mode : boolean);
  public
    constructor Create(APath, AName : string; WinShow, FileLog, WriteOver : boolean);
    destructor Destroy; override;
    procedure  Log(AModul, AFunc, AText : string);
  published
    ...
    property LogFile  : boolean read FLogFile  write SetLogFile;
    property ShowWin  : boolean read FShowWin  write SetLogWin;
    ...
  end;

constructor TLOG.Create(APath, AName : string; WinShow, FileLog, WriteOver : boolean);
var
  ....
begin
  inherited Create;
  ....
  // create the filename for read/write INI-file
  ....
  // set several var's as default if INI_read fails:
  ....
  // read INI-file, if existing ! ... maybe overwrting setted var's ! see above
  ....
  // init Timer
  FLogTimer         := TTimer.Create(Application);
  FLogTimer.Enabled := false;  // true if something to display / write
  FLogTimer.Interval := 250;    // 250ms should be fast enough
  FLogTimer.OnTimer := OnTimer;
  // only internal
  FOnMessageLock := TBCCritSec.Create;
  FLogList      := TStringList.Create;               // a 'must have'
  FLOGWIN       := NIL;
  FFileStream   := NIL;                              // no file open
  ....
  // now initalize window and / or filewriter
  SetLogWin(FShowWin);     // opens window, if true
  SetLogFile(FLogFile);    // opens file, if true
  FLogTimer.Enabled := true;
  FOnDestroy        := false;
end;

destructor TLOG.Destroy;
begin
  FOnDestroy := true;
  SetLogWin(false);      // closes window, if open
  if Assigned(FLOGWIN) then FLOGWIN.Free;
  SetLogFile(false);     // closes file, if open
  ....
  FLogTimer.Enabled := false;
  FLogTimer.Free;
  FLogList.Free;
  FOnMessageLock.Free;
  INI_write;
  inherited destroy;
end;

......

procedure TLOG.SetLogWin(Mode : boolean);
begin
  if NOT Assigned(FLOGWIN) then  // no window open / created
  begin
    if Mode then                 // we want to open / create it
    begin
      FLOGWIN        := TLOGWIN.Create(Application);
      FLOGWIN.Visible := true;       // first make visible ...
      FLOGWIN.Top    := FWinTop;    // then set pos & size !
      FLOGWIN.Left   := FWinLeft;
      FLOGWIN.Width  := FWinWidth;
      FLOGWIN.Height := FWinHeight;
      if FWinOnTop then
        FLOGWIN.FormStyle := fsStayOnTop
      else
        FLOGWIN.FormStyle := fsNormal;
      FLOGWIN.Caption := FAppName + ' - LOGGER';
      FShowWin       := true;
    end;
  end
  else                           // window is open / created
  begin
    if NOT Mode then             // we want to close / destroy it
    begin
      FWinTop   := FLOGWIN.Top;   // remind position
      FWinLeft  := FLOGWIN.Left;
      FWinWidth := FLOGWIN.Width; // remind size
      FWinHeight := FLOGWIN.Height;
      if NOT FOnDestroy then         // little helper doesnt work !
      begin
        FLOGWIN.Free;
        FLOGWIN := NIL;
      end;
      FShowWin  := false;
    end;
  end;
end;
Ich hoffe, "man" kann da ein wenig durchblicken !?

Noch mal als Anmerkung:
SetLogWin(true/false) kann ich vom Hauptprogramm x-mal aufrufen. Kein Thema ....
Der Aufruf SetLogWin(false) in LOG.Destroy geht immer in die Hose.
Auch if Assigned(FLOGWIN) then FLOGWIN.Free.

Geändert von TERWI (31. Mär 2013 um 19:39 Uhr)
  Mit Zitat antworten Zitat