Delphi窗體置頂及失去焦點后取得焦點


unit u_FrmTopMostActive;

interface

uses Winapi.Windows;

implementation

// 窗體置頂
procedure SetXwForegroundWindow(AHandle: Thandle);
var
  hFgWin: Thandle;
  hFgThread: Thandle;
begin

  ShowWindow(AHandle, SW_NORMAL);

  hFgWin := GetForegroundWindow;
  hFgThread := GetWindowThreadProcessID(hFgWin, nil);

  if AttachThreadInput(GetCurrentThreadID, hFgThread, true) then
  begin
    SetForegroundWindow(AHandle);
    AttachThreadInput(GetCurrentThreadID, hFgThread, false);
  end
  else
    SetForegroundWindow(AHandle);

end;

// 激活窗體
function SetSysFocus(AHandle: Thandle): boolean;
var
  hThreadId: Thandle;
  hFgThreadId: Thandle;
begin
  result := false;
  if not IsWindow(AHandle) then
    exit;
  hThreadId := GetWindowThreadProcessID(AHandle, nil);
  hFgThreadId := GetWindowThreadProcessID(GetForegroundWindow, nil);
  if AttachThreadInput(hThreadId, hFgThreadId, true) then
  begin
    SetFocus(AHandle);
    AttachThreadInput(hThreadId, hFgThreadId, false);
  end
  else
    SetFocus(AHandle);
  result := true;
end;

end.

使用方式

uses U_FrmTopmostActive;

//自己需要的處理完后調用,句柄測試為本窗體的句柄

    SetXwForegroundWindow(Self.Handle);
    SetSysFocus(Self.Handle);

感謝 曉不得2013老師 提供的代碼並耐心指導(群:59129236)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM