• 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)

  • 相关阅读:
    canvas制作倒计时炫丽效果
    MySQL存储过程
    SpringMVC入门
    JAVA面试/笔试经典题
    JAVA内存存储分配粗略讲解
    数据结构算法总结
    稳定排序
    Java集合框架
    Java笔试题及答案
    面向接口
  • 原文地址:https://www.cnblogs.com/liessay/p/5686338.html
Copyright © 2020-2023  润新知