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/)
-   -   Delphi Applikation Vordergrund (https://www.delphipraxis.net/61295-applikation-vordergrund.html)

dj2289 19. Jan 2006 16:01


Applikation Vordergrund
 
Ich habe folgendes Problem, ich habe mir ein kleines Programm geschrieben, welches mir beim Aufruf von Programmen
hilft. Wenn ich nun F4 drücke erscheint auch die Applikation, aber der Focus ist immer noch (außer bei ganz wenigen Programme)
auf der Anwendung, welche ich vorher angeklickt habe. Hier mal der Quellcode, vielleicht hat jemand ne Idee, wie ich den Focus
auf meine Anwendung erzwingen kann:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, Menus, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Timer1: TTimer;
    Label1: TLabel;
    procedure FormHide(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormShow(Sender: TObject);

  procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
     { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  key1: String;


implementation

const
  WH_KEYBOARD_LL = 13;

type
  PKbDllHookStruct = ^TKbDllHookStruct;
  TKbDllHookStruct = packed record
    vkCode: Cardinal;
    scanCode: Cardinal;
    flags: Cardinal;
    time: Cardinal;
  end;


var
  HookHandle:  Cardinal;
{$R *.dfm}
// Tastatur HOOK
function LLKeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
key1:='';
  if nCode = HC_ACTION then
       with PKbDllHookStruct(lParam)^ do
        key1:=(Format(' %4.4x %2.2x  %2.2x   %2.2x ', [wParam, vkCode, scanCode, flags]));
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;

// Abfrage
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (Key=VK_RETURN)then
begin
if edit1.Text='tttt' then WinExec('C:\Programme\ttt.exe',1);
if edit1.Text='quit' then close();
edit1.Text:='';
Form1.Left:=-150;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// Lässt das Fenster aus der unteren Programmleiste verschwinden
  ShowWindow( Application.Handle, SW_HIDE );
  SetWindowLong( Application.Handle, GWL_EXSTYLE,
                 GetWindowLong(Application.Handle, GWL_EXSTYLE) or
                 WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
  ShowWindow( Application.Handle, SW_SHOW );
//-------------------//
Form1.Left:=-150
end;

procedure TForm1.FormHide(Sender: TObject);
begin
  if HookHandle <> 0 then begin
    UnhookWindowsHookEx(HookHandle);
    HookHandle := 0;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Form1.SetFocus;
  Edit1.Focused;
  key1:='';
  HookHandle := SetWindowsHookEx(WH_KEYBOARD_LL, LLKeyboardHookProc, hInstance, 0);
  if HookHandle = 0 then RaiseLastOSError;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if key1=' 0100  73   3E   00 ' then
  begin
  Form1.Left:=+600;
  Form1.SetFocus;
  label1.caption:=key1;
  end;
end;
end.

th_bone 19. Jan 2006 16:22

Re: Applikation Vordergrund
 
Hi,

ich habe hier mal eine function erweitert, da ich auch das Problem hatte, dass immer wieder
doch ein anderes fenster den Focus hatte - sie prüft ob das fenster auch wirklich im vordergrund ist und reagiert mit verschiedenen massnahmen darauf - sieht zwar sicherlich komisch und übertrieben aus aber seitdem funktioniert es...

Tschö

Ralf

Delphi-Quellcode:
function AppActivate(WindowName : PChar) : boolean;
var
 myhandle: thandle;
 i: byte;
begin
  i:=0;

  try
   Result:=true;
   WindowHandle:=FindWindow(nil,WindowName);

   If (WindowHandle=0) then EnumWindows(@EnumWindowsProc,Integer(PChar(WindowName)));
   If (WindowHandle<>0) then begin
     SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
     SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
   end else Result:=false;
  except
   on Exception do Result:=false;
  end;

  // eigentlich sollte das hier schon reichen, aber dank Microschrott gibts ja für
  // alle befehle auch ausnahmen...

  delay(10);

  // Kontrolle ob Window auch wirklich im Vordergrund - versuche es nochmal max.20x
  while (windows.GetForegroundWindow<>windowhandle) and (i<20) do begin
    try
     if (i>15) then begin
       // wenn alles nichts hilft verkleinere zuerst alle Fenster
       myhandle := WindowHandle;
       while myhandle > 0 do begin
         if IsWindowVisible(myhandle) then PostMessage(myhandle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
         myhandle := GetNextWindow(myhandle, GW_HWNDNEXT);
       end;
     end;
     Result:=true;
     SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
     SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
     windows.ShowWindow(WindowHandle, SW_ShowNormal);
     windows.SetForegroundWindow(WindowHandle);
     windows.SetFocus(WindowHandle);
    except
     on Exception do Result:=false;
    end;
    inc(i);
    delay(10);
  end;
end;

Luckie 19. Jan 2006 16:24

Re: Applikation Vordergrund
 
Warum machst du das mit einem Keyboard Hook? Ein Hotkey reícht doch vollkommen aus.

dj2289 19. Jan 2006 17:55

Re: Applikation Vordergrund
 
Danke erstmal für den Code, nur wird bei mir der Fehler angezeigt, dass "WindowHandle" Undeklariert ist,
muss ich vielleicht eine Unit bei uses hinzufügen?

ManuMF 19. Jan 2006 18:02

Re: Applikation Vordergrund
 
Nein, einfach in die Variablen der Funktion aufnehmen.

Delphi-Quellcode:
function AppActivate(WindowName : PChar) : boolean;
var
  myhandle: thandle;
  i: byte;
  WindowHandle: hWnd;
begin
...
Gruß,
ManuMF

dj2289 19. Jan 2006 18:07

Re: Applikation Vordergrund
 
Das erste Problem ist nun beseitigt, nun kommt der Fehler, dass @EnumWindowsProc nicht deklariert ist


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

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