Unitu_frmtopmostactive;Interfaceuseswinapi.windows;Implementation//form pinnedprocedureSetxwforegroundwindow (ahandle:thandle);varHfgwin:thandle; Hfgthread:thandle;beginShowWindow (Ahandle, sw_normal); Hfgwin:=GetForegroundWindow; Hfgthread:= GetWindowThreadProcessId (Hfgwin,Nil); ifAttachThreadInput (GetCurrentThreadID, Hfgthread, True) Then beginSetForegroundWindow (Ahandle); AttachThreadInput (GetCurrentThreadID, Hfgthread, false); End ElseSetForegroundWindow (ahandle);End;//Activate formfunctionSetsysfocus (Ahandle:thandle): boolean;varHthreadid:thandle; Hfgthreadid:thandle;beginResult:=false; if notIsWindow (Ahandle) Thenexit; Hthreadid:= GetWindowThreadProcessId (Ahandle,Nil); Hfgthreadid:= GetWindowThreadProcessId (GetForegroundWindow,Nil); ifAttachThreadInput (Hthreadid, Hfgthreadid, True) Then beginSetFocus (Ahandle); AttachThreadInput (Hthreadid, Hfgthreadid, false); End ElseSetFocus (Ahandle); Result:=true;End;End.
How to use
uses u_frmtopmostactive;
The handle of the handle test for this form is called when the handle is finished.
Setxwforegroundwindow (Self.handle);
Setsysfocus (Self.handle);
Thank you, Xiao. 2013 the code provided by the teacher and patient guidance (group: 59129236)
Delphi form highlights and loses focus after focus