Einzelnen Beitrag anzeigen

Benutzerbild von paule32.jk
paule32.jk

Registriert seit: 24. Sep 2022
Ort: Planet Erde
356 Beiträge
 
Delphi 11 Alexandria
 
#1

Delphi 21.0 - Send keys funktioniert nicht

  Alt 13. Okt 2023, 19:45
Hallo,
ich habe den folgenden Code (unten):
Ich würde gerne in eine anderen Anwendung Text kopieren, der dann formatiert
in eine RichEdit Komponente übertragen werden soll (also mit Font + Farbe):
Ich habe bereits (nicht funktionierenden) Code:

Delphi-Quellcode:
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, TLHelp32, Clipbrd,
  System.Generics.Collections, Vcl.ComCtrls;


type
  TForm2 = class(TForm)
    Button1: TButton;
    RichEdit1: TRichEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

type
  PWindows = ^TWindows;
  TWindows = record
    WindowHandle: HWND;
    WindowText: string;
  end;
var
  AWindows: PWindows;

implementation

{$R *.dfm}

function ArrayToString(const a: array of Char): string;
begin
  if Length(a)>0 then
    SetString(Result, PChar(@a[0]), Length(a))
  else
    Result := '';
end;

function GetRichViewText(hWnd: HWND): string;
var
  textLength: Integer;
  text: String;
begin
  // Erstelle ein Puffer für den Text
  textLength := SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
  SetLength(text, textLength + 1);

  // Hole den Text aus dem Fenster
  SendMessage(hWnd, WM_GETTEXT, textLength + 1, LPARAM(PChar(text)));

  Result := String(text);
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  hSnap: THandle;
  ProcEntry: TProcessEntry32;
  s: string;
  found: Boolean;
  win,w2,tmp: HWND;
  wl, i: LongInt;
  Input: TInput;
  InputList: TList<TInput>;

  function GetWindowHandle(ProcessId: Cardinal): THandle;
  var hFound: THandle;
    function EnumWindowsProcMy(_hwnd: HWND; ProcessId: Cardinal): BOOL; stdcall;
    var dwPid: Cardinal;
    begin
      GetWindowThreadProcessId(_hwnd, @dwPid);
      if ProcessId = dwPid then
      begin
        hFound := _hwnd;
        Result := False;
      end else
      Result := True;
    end;
  begin
    EnumWindows(@EnumWindowsProcMy, LPARAM(ProcessId));
  end;
begin
  RichEdit1.Lines.Clear;

  found := false;
  hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

  if (hSnap <> INVALID_HANDLE_VALUE) then
  begin
    ProcEntry.dwSize := SizeOf(ProcessEntry32);
    if (Process32First(hSnap, ProcEntry)) then
    begin
      while Process32Next(hSnap, ProcEntry) do
      begin
        s := ProcEntry.szExeFile;
        if ExtractFileName(s) = 'hnd8.exethen
        begin
          win := GetWindowHandle(ProcEntry.th32ProcessID);
          tmp := win;
          if win <> 0 then
          begin
            for i := 0 to $1000000 do
            begin
              w2 := GetWindow(i, 2);
              wl := GetWindowLong(w2,GWL_ID);
              if wl = 135098 then
              begin
                found := true;
                break;
              end;
            end;
            if not found then
            begin
              ShowMessage('internal hnd8 error.');
              exit;
            end;
            showmessage('win: ' + inttohex(tmp));
            Winapi.Windows.ShowWindow(tmp,SW_MAXIMIZE);
            Sleep(10000);
            showmessage('xuxu');
            // select all: ctrl+A ...
            InputList := TList<TInput>.Create;
            try
              Input := Default(TInput);
              Input.Itype := INPUT_KEYBOARD;
              Input.ki.wScan := 0;
              Input.ki.time := 0;
              Input.ki.dwExtraInfo := 0;

              // 1. press ctrl key
              Input.ki.dwFlags := 0;
              Input.ki.wVk := VK_CONTROL;
              InputList.Add(Input);

              // 2. press "a" key
              Input.ki.dwFlags := 0;
              Input.ki.wVk := Ord('A');
              InputList.Add(Input);

              // 3. release "a" key
              Input.ki.dwFlags := KEYEVENTF_KEYUP;
              Input.ki.wVk := Ord('A');
              InputList.Add(Input);

              // 4. release ctrl key
              Input.ki.dwFlags := KEYEVENTF_KEYUP;
              Input.ki.wVk := VK_CONTROL;
              InputList.Add(Input);

              SendInput(InputList.Count, InputList.List[0], sizeof(TInput));
              Sleep(500);

              //s := GetRichViewText(w2);
              //Memo1.Lines.Add(s);
            finally
              InputList.Free;
            end;

            // copy selected text: ctrl+c
            InputList := TList<TInput>.Create;
            try
              Input := Default(TInput);
              Input.Itype := INPUT_KEYBOARD;
              Input.ki.wScan := 0;
              Input.ki.time := 0;
              Input.ki.dwExtraInfo := 0;

              // 1. press ctrl key
              Input.ki.dwFlags := KEYEVENTF_UNICODE;
              Input.ki.wVk := VK_CONTROL;
              InputList.Add(Input);

              // 2. press "c" key
              Input.ki.dwFlags := KEYEVENTF_UNICODE;
              Input.ki.wVk := Ord('C');
              InputList.Add(Input);

              // 3. release "c" key
              Input.ki.dwFlags := KEYEVENTF_KEYUP;
              Input.ki.wVk := Ord('C');
              InputList.Add(Input);

              // 4. release ctrl key
              Input.ki.dwFlags := KEYEVENTF_KEYUP;
              Input.ki.wVk := VK_CONTROL;
              InputList.Add(Input);

              SendInput(InputList.Count, InputList.List[0], sizeof(TInput));
              Sleep(500);

              RichEdit1.Perform(WM_SETTEXT, 0, PWChar(Clipboard.AsText));

              //s := GetRichViewText(w2);
              //Memo1.Lines.Add(s);
            finally
              InputList.Free;
            end;
            break;
          end;
        end;
      end;
    end;
  end;
  CloseHandle(hSnap);
end;
Frag doch einfach
Alles was nicht programmiert werden kann, wird gelötet
  Mit Zitat antworten Zitat