我正在编写此代码以从另一个应用程序(进程)中选择一些文本行,但问题是我无法处理此应用程序并获取所选文本完美选择的文本但无法复制此文本,是否存在有什么方法可以在 delphi 中模拟 Ctrl+C 命令?这是我的代码
SetCursorPos(300, 300);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
SetCursorPos(300, 350);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
if not AttachThreadInput(GetCurrentThreadId,
GetWindowThreadProcessId(GetForegroundWindow), true) then
RaiseLastOSError;
try
SendMessage(GetFocus, WM_GETTEXT, 0, 0);
lookup_word := clipboard.astext;
CurvyEdit1.Text := lookup_word;
finally
AttachThreadInput(GetCurrentThreadId,
GetWindowThreadProcessId(GetForegroundWindow), false);
end;
Run Code Online (Sandbox Code Playgroud)
WM_GETTEXT直接检索实际文本,它不会将文本放在剪贴板上。 WM_COPY而是这样做。使用 时WM_GETTEXT,您必须为要复制到的文本提供字符缓冲区。你不是在这样做。
所以要么WM_GETTEXT正确使用:
var
lookup_word: string;
Wnd: HWND;
Len: Integer;
lookup_word := '';
Wnd := GetFocus;
if Wnd <> 0 then
begin
Len := SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0);
if Len > 0 then
begin
SetLength(lookup_word, Len);
Len := SendMessage(Wnd, WM_GETTEXT, Len+1, LPARAM(PChar(lookup_word)));
SetLength(lookup_word, Len);
end;
end;
CurvyEdit1.Text := lookup_word;
Run Code Online (Sandbox Code Playgroud)
或者使用 WM_COPY 代替:
SendMessage(GetFocus, WM_COPY, 0, 0);
Run Code Online (Sandbox Code Playgroud)