GETTEXT不适用于使用delphi的其他进程

GETTEXT不适用于使用delphi的其他进程

本文介绍了WM_GETTEXT不适用于使用delphi的其他进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写此代码,以便从另一个应用程序(流程)中选择一些文本行但问题是我无法处理此应用程序并获取所选文本完美选择的文本,但无法复制此文本,有什么方法可以模拟Ctrl + C在delphi中命令?这是我的代码

I'm writing this code to select some lines of text from another application ( process )but the problem is that i can't handel to this application and get the selected textthe text selected perfectly but can't copy this text , is there any way to simulate Ctrl+Ccommand in delphi ? THis is my code

    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;

推荐答案

WM_GETTEXT 直接检索实际文本,它不会将文本放在剪贴板上. WM_COPY 代替了.使用 WM_GETTEXT 时,必须为要复制的文本提供一个字符缓冲区.你不是那样做的.

WM_GETTEXT retrieves the actual text directly, it does not put the text on the clipboard. WM_COPY does that instead. When using WM_GETTEXT, you must provide a character buffer for the text to be copied into. You are not doing that.

因此可以正确使用 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:

Or else use WM_COPY instead:

SendMessage(GetFocus, WM_COPY, 0, 0);

这篇关于WM_GETTEXT不适用于使用delphi的其他进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:13