zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    寒假学习进度15
    寒假学习进度14
    寒假学习进度13
    Markdown使用笔记
    MVC
    阅读笔记大型网站技术架构01
    周总结1大数据采集技术与应用(徳拓)五次实验总结
    阅读笔记架构漫谈03
    质量属性易用性分析
    阅读笔记架构漫谈02
  • 原文地址:https://www.cnblogs.com/jijm123/p/13788121.html
Copyright © 2011-2022 走看看