Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   TCustomControl: Focus erhalten wenn Programm aktiv wird (https://www.delphipraxis.net/189846-tcustomcontrol-focus-erhalten-wenn-programm-aktiv-wird.html)

DCoderHH 29. Jul 2016 13:42

TCustomControl: Focus erhalten wenn Programm aktiv wird
 
Hallo,

ich habe mir ein TCustomControl erstellt. Dieses muss folgende Fälle unterscheiden können:

1) Das Control hat durch einen Mausklick den Fokus erhalten
erkenne ich über MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

2) Das Control hat durch die Tab-Taste den Fokus erhalten
erkenne ich über WMSetFocus(...) und dadurch dass MouseDown(...) wurde nicht aufgerufen wurde

3) Das Control hat den Fokus erhalten, als das Programm aktiv wurde
(vorher war ein anderes Programm aktiv, dann wurde das eigene Programm aktiv durch Alt+Tab oder einen Mausklick (nicht auf das TCustomControl))

Wie kann ich nun Fall 3 erkennen?

Sir Rufo 30. Jul 2016 11:33

AW: TCustomControl: Focus erhalten wenn Programm aktiv wird
 
Indem du auf Delphi-Referenz durchsuchenTApplicationEvents.OnActivate hörst?

TOndrej 30. Jul 2016 12:32

AW: TCustomControl: Focus erhalten wenn Programm aktiv wird
 
Zum Beispiel:

Delphi-Quellcode:
type
  TMyControl = class(TCustomControl)
  protected
    function MainWndHook(var Message: TMessage): Boolean;
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

constructor TMyControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if not (csDesigning in ComponentState) then
    Application.HookMainWindow(MainWndHook);
end;

destructor TMyControl.Destroy;
begin
  if not (csDesigning in ComponentState) then
    Application.UnhookMainWindow(MainWndHook);
  inherited Destroy;
end;

function TMyControl.MainWndHook(var Message: TMessage): Boolean;
begin
  case Message.Msg of
    CM_ACTIVATE,
    CM_DEACTIVATE:
      Invalidate;
  end;
  Result := False;
end;

procedure TMyControl.Paint;
const
  Colors: array[Boolean] of TColor = (clSilver, clRed);
begin
  Canvas.Brush.Color := Colors[(csDesigning in ComponentState) or Application.Active];
  Canvas.Brush.Style := bsSolid;
  Canvas.FillRect(ClientRect);
end;

DCoderHH 3. Aug 2016 07:36

AW: TCustomControl: Focus erhalten wenn Programm aktiv wird
 
@TOndrej: Danke, das ist genau das was ich gesucht habe. Es funktioniert und kann in der Komponenten gekapselt werden.

TOndrej 3. Aug 2016 19:12

AW: TCustomControl: Focus erhalten wenn Programm aktiv wird
 
Gern geschehen! :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:11 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz