我想要一个外部控件的编辑。应用程序是用Delphi编写的。它有几种形式。我从Python库pywinauto
+sendkeys
开始测试第一个形式TLoginForm
。它工作得很好。以下是伪代码:
helper = pywinauto.application.Application()
hwnd = pywinauto.findwindows.find_windows(class_name='TLoginForm')[0]
window = helper.window_(handle=hwnd)
ctrl = window[2] # the second control is the edit control I want to access
ctrl.ClickInput() # focus the control
ctrl.SetEditText('Hello world') # text can be changed expectedly
作为第二步,我想为自动化工具创建一个UI。但是由于缺乏pythonui的知识,考虑到Python中二进制文件分发的复杂性,我想用Delphi来做。但奇怪的是,我无法使用Windows api在Delphi中读/写编辑控件。以下是一些尝试:
^{pr2}$由于它在Python中工作,我想我一定错过了一些非常基本和非常重要的东西。但我现在太盲目了,找不到问题所在。如有任何提示,我们将不胜感激。在
pywinauto使用标准的win32api,所以它能做的任何事情,都可以在Delphi中完成。在
pywinauto is open source,因此您可以看到}是如何实现的。在
ctrl.ClickInput()
和{ctrl.ClickInput()
调用^{ctrl.SetEditText()
发送一条^{另外需要注意的是,pywinauto倾向于在其他窗口/进程中执行操作后调用},给目标一些时间来处理这些操作。这可能是“反输入保护”试图清除自动代码但允许用户活动的一个因素。在
WaitForInputIdle()
和{我从未听说过
SetForegroundWindow()
将子控件带到前台。即使是这样,SetForegroundWindow()
也有许多限制,这些限制可能会阻止应用程序设置前景窗口。在如果要将输入焦点更改为另一个进程中的窗口,则必须使用} documentation 中有明确说明。在
AttachThreadInput()
将调用线程附加到目标窗口的线程。这在^{SetText()
不是标准的Win32 API函数。你是说^{SetWindowText()
不能在另一个进程中设置窗口的文本,文档也这么说。在或者
SetText()
是WM_SETTEXT
的包装?具有“anti-input protection”的控件可能会阻止它自己不生成的WM_SETTEXT
消息。在SendKeys只是将击键放入系统的键盘队列中,让Windows将它们传递到焦点窗口。这应该是可行的,因为应用程序无法区分用户输入的击键和SendKeys注入的击键。除非目标应用程序挂接
SendKeys()
和keybd_event()
来检测注入的击键,也就是说。在你试过这个代码了吗?在
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27432926.html
您有
Msg
和wParam
参数值。Ord('H')
是72,这是WM_POWER
消息。编辑控件不关心电力状态的更改。在发送这些消息时,还需要包含一些标志:
如果使用
AttachThreadInput()
,则需要附加到拥有编辑控件的线程,因此使用编辑控件的HWND,而不是其父HWND。在您使用的是什么版本的Windows?在Vista和更高版本上,
GetLastError()
如果AttachThreadInput()
失败,则返回一个有效的错误代码。在更新:在Delphi中,您展示的脚本的pywinauto源代码的大致翻译如下:
^{pr2}$相关问题 更多 >
编程相关推荐