【发布时间】: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。 " 你是从正确的线程调用它吗,即拥有窗口的线程?