-2

我正在编写此代码以从另一个应用程序(进程)中选择一些文本行,但问题是我无法处理此应用程序并获取所选文本完美选择的文本但无法复制此文本,是否存在有什么方法可以在 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;
4

3 回答 3

5

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;

或者使用 WM_COPY 代替:

SendMessage(GetFocus, WM_COPY, 0, 0);
于 2013-07-11T22:48:04.213 回答
1

尝试向目标窗口(编辑控件)句柄发送WM_COPY消息。

于 2013-07-11T22:36:04.117 回答
1

它工作正常。您已在其他相关问题中表明您正在使用记事本- 这将从记事本的运行实例中检索所有文本(它找到的第一个!)并将其放入TMemoDelphi 表单中。在 64 位的 Windown 7 上在 Delphi 2007 中测试。

procedure TForm1.Button3Click(Sender: TObject);
var
  NpWnd, NpEdit: HWnd;
  Buffer: String;
  BufLen: Integer;
begin
  Memo1.Clear;
  NpWnd := FindWindow('Notepad', nil);
  if NpWnd <> 0 then
  begin
    NpEdit := FindWindowEx(NpWnd, 0, 'Edit', nil);
    if NpEdit <> 0 then
    begin
      BufLen := SendMessage(NpEdit, WM_GETTEXTLENGTH, 0, 0);
      SetLength(Buffer, BufLen + 1);
      SendMessage(NpEdit, WM_GETTEXT, BufLen + 1, LParam(PChar(Buffer)));
      Memo1.Lines.Text := Buffer;
    end;
  end;
end;
于 2013-07-11T23:48:13.650 回答