Entwickler-Ecke

Windows API - Windows.GetFocus funktioniert bei Fremdanwendungen nicht


Jakob Schöttl - Fr 08.12.06 23:56
Titel: Windows.GetFocus funktioniert bei Fremdanwendungen nicht
Hi,
Ich habe einen Timer, der nach drei Sekunden Windows.GetFocus abfrägt, und anzeigt. Wenn der Curser in meiner eigenen anwendung bleibt, dann bringt die funktion einen akzeptablen wert. Wenn ich aber den Curser in eine Fremdanwendung setzte, dann ist das Ergebnis immer 0.

Ich hoffe ihr könnt mir helfen, danke schon mal
Jakob


Delete - Sa 09.12.06 02:16

Lesen bildet:
Zitat:
The GetFocus function retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.


Abhilfe: Suche im MSDN ATTACHTHREADINPUT.


Jakob Schöttl - Sa 09.12.06 09:43

Kannst du mir bitte noch sagen, was die ThreadID ist, und wo die eigene gespeichert ist, und mit welcher funktion ich die vom Ziel-Fenster rauskriegt?

Ich hab zwar hier bei ActiveVB [http://www.activevb.de/tipps/vb6tipps/tipp0247.html] VisualBasic Code gefunden, aber der hilft mir nur beschränkt weiter.


Delete - Sa 09.12.06 12:24

Suche im MSDN GETWINDOWTHREADPROCESSID, Suche im MSDN GETCURRENTTHREADID.


Jakob Schöttl - Mi 13.12.06 14:33

Danke Luckie. Jetzt hab ichs so geschafft:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var OtherThreadID: Cardinal;
    TargetNil: Boolean;
//...  
begin
  Thread1 := GetWindowThreadProcessId(Self.Handle, OtherThreadID);
  Thread2 := GetWindowThreadProcessId(GetForegroundWindow, OtherThreadID);
  TargetNotNil := False;
  If Thread1 <> Thread2 Then TargetNotNil := AttachThreadInput(Thread2,Thread1, True);
  if TargetNotNil then begin
    Options.TargetHandle := Windows.GetFocus;
      //jetzt weiß man das Handle des Ziels...
    end;    
end;