加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby win32-api GetFocus和GetWindowText

发布时间:2020-12-17 02:03:51 所属栏目:百科 来源:网络整理
导读:我正试图找到一种获取当前窗口文本的方法. 所以使用win32-api gem我使用 this page的一些帮助编写了这段代码 require 'win32/api'include Win32hWnd = GetActiveWindow = API.new('GetActiveWindow','V','L','user32').callGetWindowText = API.new('GetWind
我正试图找到一种获取当前窗口文本的方法.
所以使用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','user32')
buf_len = GetWindowTextLength.call(hwnd)
str = ' ' * (buf_len + 1)
# Retreive the text.
result = GetWindowText.call(hwnd,str,str.length)
puts str.strip

输出只是一个空字符串,因为由于hwnd设置为0,buf_len总是计算为0.
我无法弄清楚为什么返回的hwnd总是只有0.

解决方法

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

Retrieves a handle to the foreground window (the window with which the user is currently working).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读