【问题标题】:Get handle of focused control from another applications window从另一个应用程序窗口获取集中控制的句柄
【发布时间】:2014-02-05 12:30:05
【问题描述】:

我有一个应用程序有窗口这个一些控件(按钮,编辑等)。我需要模拟用户事件(如 Tab 点击和输入文本)。我正在使用keybd_event 在选项卡有序控件(编辑框)之间移动焦点并向它们输入文本。但我需要知道当前焦点控件的句柄(例如从中获取文本或更改其样式)。我该如何解决?

ps 我现在正在写 Delphi 但没关系(Win-API 到处都一样)。

【问题讨论】:

  • 看看GetGuiThreadInfo()
  • 您为什么决定伪造输入而不是使用自动化 API?
  • @DavidHeffernan:很好奇 - 你想到了哪个自动化 API?
  • @MartynA UIAutomation
  • David,我只需要使用 WinApi,无需 3-d 派对解决方案。我在公司工作,所以这不是我的决定

标签: c++ c windows delphi


【解决方案1】:

请参阅 GetFocus' 文档中的备注部分以了解以下示例的说明。

function GetFocus: HWND;
var
  Wnd: HWND;
  TId, PId: DWORD;
begin
  Result := windows.GetFocus;
  if Result = 0 then begin
    Wnd := GetForegroundWindow;
    if Wnd <> 0 then begin
      TId := GetWindowThreadProcessId(Wnd, PId);
      if AttachThreadInput(GetCurrentThreadId, TId, True) then begin
        Result := windows.GetFocus;
        AttachThreadInput(GetCurrentThreadId, TId, False);
      end;
    end;
  end;
end;

【讨论】:

    【解决方案2】:

    来自 Win-API 的GetDlgItem 返回一个值,该值是指定控件的窗口句柄。

    【讨论】:

      【解决方案3】:

      我将 Sertac Akyuz 的 pascal 代码转换为 c++

      #include "Windows.h"
      #include <psapi.h> // For access to GetModuleFileNameEx
      #include <iostream>
      #include <string>
      
      
      #ifdef _UNICODE
      #define tcout wcout
      #define tcerr wcerr
      #else
      #define tcout cout
      #define tcerr cerr
      #endif
      
      HWND GetFocusGlobal()
      {
          HWND Wnd;
          HWND Result = NULL;
          DWORD TId, PId;
      
          Result = GetFocus();
          if (!Result)
          {
              Wnd = GetForegroundWindow();
              if(Wnd)
              {
                  TId = GetWindowThreadProcessId(Wnd, &PId);
                  if (AttachThreadInput(GetCurrentThreadId(), TId, TRUE))
                  {
                      Result = GetFocus();
                      AttachThreadInput(GetCurrentThreadId(), TId, FALSE);
                  }            
              }
          }
          return Result;
      }
      
      
      int _tmain(int argc, _TCHAR* argv[])
      {
          std::wstring state;
      
          while(1)
          {
              HWND focus_handle = GetFocusGlobal();
      
              if(focus_handle)
              {
      
                  TCHAR text[MAX_PATH];
                  GetClassName(focus_handle, text, MAX_PATH);
      
                  const std::wstring cur_path(text);
      
                  if(cur_path != state)
                  {
                      std::tcout << "new:" << focus_handle << " " << text << std::endl;
                      state = cur_path;
                  }
      
                  }
      
              Sleep(50); // Sleep for 50 ms.
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多