但奇怪的是我无法使用 Windows api 在 Delphi 中读取/写入编辑控件。
pywinauto 使用标准的 Win32 API,所以它可以做的任何事情,你都可以在 Delphi 中做。
pywinauto 是开源的,所以你可以看到是如何实现的ctrl.ClickInput()
。ctrl.SetEditText()
ctrl.ClickInput()
调用SetCursorPos()
和SendInput()
。
ctrl.SetEditText()
发送一条EM_SETSEL
消息以突出显示 Edit 控件的当前文本,然后发送一条EM_REPLACESEL
消息以用新文本替换突出显示的文本。我的猜测是编辑控件的“反输入保护”可能不会阻止这些消息。
还有一点需要注意的是,pywinauto 倾向于在其他窗口/进程中执行操作之后调用WaitForInputIdle()
并Sleep()
给目标一些时间来处理这些操作。这可能是“反输入保护”试图清除自动代码但允许用户活动的一个因素。
SetForegroundWindow(EditControlHandle); // 工作,应用程序将被带到前面,编辑控件将被聚焦
我从未听说过SetForegroundWindow()
将子控件置于前台。即使是这样,SetForegroundWindow()
也有许多限制可能会阻止您的应用程序设置前景窗口。
SetFocus(EditControlHandle); // 如果当前焦点在表单的另一个编辑控件上,则什么也不会发生
如果要将输入焦点更改为另一个进程中的窗口,则必须使用 . 将调用线程附加到目标窗口的线程AttachThreadInput()
。SetFocus()
这在文档中有明确说明。
SetText(EditControlHandle, 'Hello world'); // 什么都没发生
SetText()
不是标准的 Win32 API 函数。你的意思是SetWindowText()
相反? SetWindowText()
无法在另一个进程中设置窗口的文本,文档也说了这么多。
或者是SetText()
一个包装器WM_SETTEXT
?具有“反输入保护”的控件可能会阻止WM_SETTEXT
它自己不生成的消息。
SendKeys32.SendKey('Hello world', {Wait=}True); // 什么都没发生
SendKeys 只是将击键放入系统的键盘队列,让 Windows 将它们传递到焦点窗口。这应该可行,因为应用程序无法区分用户输入的击键和 SendKeys 注入的击键。除非目标应用程序正在挂钩SendKeys()
并keybd_event()
检测注入的击键,否则就是这样。
你试过这个代码了吗?
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27432926.html
SendMessage(EditControlHandle, Ord('H'), WM_KEYDOWN, 0); // 什么也没有发生
SendMessage(EditControlHandle, Ord('H'), WM_KEYUP, 0);
你有向后的Msg
和wParam
参数值。Ord('H')
是 72,这是WM_POWER
消息。编辑控件不关心电力状态的变化。
发送这些消息时,您还需要包含一些标志:
var
ScanCode: UINT;
ScanCode := MapVirtualKey(Ord('H'), MAPVK_VK_TO_VSC);
SendMessage(EditControlHandle, WM_KEYDOWN, Ord('H'), ScanCode shl 16);
SendMessage(EditControlHandle, WM_KEYUP, Ord('H'), (ScanCode shl 16) or $C0000001);
FocusedThreadID := GetWindowThreadProcessID(ExternalAppMainWindowHandle, nil);
如果使用AttachThreadInput()
,则需要附加到拥有 Edit 控件的线程,因此请使用 Edit 控件的 HWND,而不是其父 HWND。
if AttachThreadInput(GetCurrentThreadID, FocusedThreadID, {Attach=}True) then // 返回 False
您使用的是什么版本的 Windows?在 Vista 及更高版本上,如果失败,GetLastError()
则返回有效的错误代码。AttachThreadInput()
更新:您显示的脚本的 pywinauto 源代码的粗略翻译在 Delphi 中看起来像这样:
uses
..., Windows;
procedure WaitGuiThreadIdle(wnd: HWND);
var
process_id: DWORD;
hprocess: THandle;
begin
GetWindowThreadProcessId(wnd, process_id);
hprocess := OpenProcess(PROCESS_QUERY_INFORMATION, 0, process_id);
WaitForInputIdle(hprocess, 1000);
CloseHandle(hprocess);
end;
function SndMsgTimeout(wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): DWORD_PTR;
begin
SendMessageTimeout(wnd, Msg, wParam, lParam, SMTO_NORMAL, 1, @Result);
end;
var
wnd, ctrl, cur_foreground: HWND;
cur_fore_thread, control_thread: DWORD;
r: TRect;
input: array[0..1] of TInput;
i: Integer;
begin
// hwnd = pywinauto.findwindows.find_windows(class_name='TLoginForm')[0]
wnd := FindWindow('TLoginForm', nil);
// window = helper.window_(handle=hwnd)
// ctrl = window[2] # the second control is the edit control I want to access
wnd := GetWindow(wnd, GW_CHILD);
ctrl := GetWindow(wnd, GW_HWNDNEXT);
// ctrl.ClickInput() # focus the control
cur_foreground := GetForegroundWindow();
if ctrl <> cur_foreground then
begin
cur_fore_thread := GetWindowThreadProcessId(cur_foreground, nil);
control_thread := GetWindowThreadProcessId(ctrl, nil);
if cur_fore_thread <> control_thread then
begin
AttachThreadInput(cur_fore_thread, control_thread, True);
SetForegroundWindow(ctrl);
AttachThreadInput(cur_fore_thread, control_thread, False);
end
else
SetForegroundWindow(ctrl);
WaitGuiThreadIdle(ctrl);
Sleep(60);
end;
GetWindowRect(ctrl, r);
SetCursorPos((r.Width div 2) + r.Left, (r.Height div 2) + r.Top);
Sleep(10);
for I := 0 to 1 do
begin
input[I].Itype := INPUT_MOUSE;
input[I].mi.dx := 0;
input[I].mi.dy := 0;
input[I].mi.mouseData := 0;
input[I].mi.dwFlags := 0;
input[I].mi.time := 0;
input[I].mi.dwExtraInfo := 0;
end;
if GetSystemMetrics(SM_SWAPBUTTON) = 0 then
begin
input[0].mi.dwFlags := MOUSEEVENTF_LEFTDOWN;
input[1].mi.dwFlags := MOUSEEVENTF_LEFTUP;
end else
begin
input[0].mi.dwFlags := MOUSEEVENTF_RIGHTDOWN;
input[1].mi.dwFlags := MOUSEEVENTF_RIGHTUP;
end;
for I := 0 to 1 do
begin
SendInput(1, @input[I], Sizeof(TInput));
Sleep(10);
end;
// ctrl.SetEditText('Hello world') # text can be changed expectedly
SndMsgTimeout(ctrl, EM_SETSEL, 0, -1);
WaitGuiThreadIdle(ctrl);
Sleep(0);
SndMsgTimeout(ctrl, EM_REPLACESEL, 1, LPARAM(PChar('Hello world')));
WaitGuiThreadIdle(ctrl);
Sleep(0);
end;