【问题标题】:Ruby win32-api GetFocus and GetWindowTextRuby win32-api GetFocus 和 GetWindowText
【发布时间】:2013-01-09 03:39:01
【问题描述】:

我正在尝试找到一种方法来获取当前窗口的文本。 因此,我使用 win32-api gem 使用 this page 的一些帮助编写了这段代码

require 'win32/api'
include Win32
hWnd = GetActiveWindow = API.new('GetActiveWindow', 'V', 'L', 'user32').call
GetWindowText       = API.new('GetWindowText', 'LPI', 'I', 'user32')
GetWindowTextLength = API.new('GetWindowTextLength', 'L', 'I', 'user32')
buf_len = GetWindowTextLength.call(hwnd)
str = ' ' * (buf_len + 1)
# Retreive the text.
result = GetWindowText.call(hwnd, str, str.length)
puts str.strip

输出只是一个空字符串,因为 buf_len 总是计算为 0,因为 hwnd 设置为 0。 我不明白为什么返回的 hwnd 总是只是一个 0。

【问题讨论】:

  • MSDN 声明 “返回值是附加到 调用线程 的消息队列的活动窗口的句柄。否则,返回值为 NULL。 " 你是从正确的线程调用它吗,即拥有窗口的线程?

标签: ruby winapi


【解决方案1】:

如前所述,GetActiveWindow 只会检索附加到调用线程的消息队列的窗口。如果您想获取用户当前所在窗口的句柄,而不管它在哪个进程中运行,请尝试调用GetForegroundWindow 而不是GetActiveWindow

检索前台窗口(用户当前正在使用的窗口)的句柄。

【讨论】:

  • 我没有意识到我是怎么错过的。非常感谢你和蒂姆!
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多