Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#5

AW: Objekt, was zuvor den Fokus hatte ermitteln

  Alt 21. Sep 2012, 15:29
Nimm diese Form als Template für alle anderen Forms und du hast:
  • eine Eigenschaft LastActiveControl
  • ein Event OnFocusChanged
  • eine Methode BackToLastActiveControl
Delphi-Quellcode:
unit ViewTemplate;

interface

uses
  Winapi.Windows,
  System.Classes,
  Vcl.Forms, Vcl.Controls;

type
  TfrmTemplate = class( TForm )
  private
    FCurrentControl : TWinControl;
    FLastActiveControl : TWinControl;
    FOnFocusChanged : TNotifyEvent;
  protected
    procedure CMFocusChanged( var Message : TCMFocusChanged ); message CM_FOCUSCHANGED;
  public
    function BackToLastActiveControl : Boolean;
  published
    property LastActiveControl : TWinControl read FLastActiveControl;
    property OnFocusChanged : TNotifyEvent read FOnFocusChanged write FOnFocusChanged;
  end;

var
  frmTemplate : TfrmTemplate;

implementation

{$R *.dfm}
{ TfrmTemplate }

function TfrmTemplate.BackToLastActiveControl : Boolean;
begin
  Result := False;
  if Assigned( LastActiveControl ) and LastActiveControl.CanFocus
  then
    begin
      LastActiveControl.SetFocus;
      Result := True;
    end;
end;

procedure TfrmTemplate.CMFocusChanged( var Message : TCMFocusChanged );
begin
  FLastActiveControl := FCurrentControl;
  FCurrentControl := ActiveControl;

  if Assigned( OnFocusChanged )
  then
    OnFocusChanged( Self );
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat