// get content of "open" hnd8.exe process content editor
procedure TForm2.Button1Click(Sender: TObject);
var
hSnap : THandle;
hProc : THandle;
procWin : THandle;
priClass : DWORD;
procEntry: TProcessEntry32;
childWindowHandles: HWNDArray;
i : Integer;
s1, s2, s3:
string;
found: Boolean;
Input: TInput;
InputList: TList<TInput>;
begin
FEditorFrame.RichEdit1.Lines.Clear;
FCounter := -1;
// make a snapshot of the current system
found := false;
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnap = INVALID_HANDLE_VALUE
then
begin
ShowMessage('
Error: CreateToolhelp32Snapshot');
exit;
end;
// clean init the ProcEntry structure:
ProcEntry.dwSize := sizeOf(ProcessEntry32);
// if the processID > 0 ...
if (Process32First(hSnap, ProcEntry))
then
begin
// ... then get the running processes
while Process32Next(hSnap, ProcEntry)
do
begin
// if HelpNDoc.exe (hnd8.exe) is found, then
// internal switch to the process
s1 := ProcEntry.szExeFile;
if ExtractFileName(s1) = '
hnd8.exe'
then
begin
priClass := 0;
hProc := OpenProcess(
PROCESS_ALL_ACCESS, FALSE,
ProcEntry.th32ProcessID);
// get some internal informations about the process:
priClass := GetPriorityClass( hProc );
if priClass < 1
then
ShowMessage('
Error: GetPriorityClass');
CloseHandle(hProc);
// fill the sub-hwnd container:
childWindowHandles := FindChildWindowHandles(ProcEntry.th32ProcessID);
// now, we can iterate the sub-windows:
for i := 0
to High(childWindowHandles)
do
begin
s2 := GetWindowCaption(childWindowHandles[i]);
if System.SysUtils.AnsiPos('
- HelpNDoc Personal',s2) = 0
then
begin
if System.SysUtils.AnsiPos('
- HelpNDoc',s2) > 0
then
begin
procWin := childWindowHandles[i];
s3 := Copy(s2,1,Pos('
-',s2)-1);
found := true;
//ShowMessage('>' + s3 + '<');
break;
end;
end;
end;
end;
end;
if found = true
then
begin
Winapi.Windows.ShowWindow(procWin,SW_RESTORE);
Winapi.Windows.SetForegroundWindow(procWin);
// 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;
// 0 for key-press
Input.ki.wVk := VK_CONTROL;
InputList.Add(Input);
// 2. press "a" key
Input.ki.dwFlags := 0;
// 0 for key-press
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));
//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 := 0;
// 0 for key-press
Input.ki.wVk := VK_CONTROL;
InputList.Add(Input);
// 2. press "c" key
Input.ki.dwFlags := 0;
// 0 for key-press
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);
// copy text to clipboard, sleep could be adjusted for big text:
SendInput(InputList.Count, InputList.List[0], sizeof(TInput));
Sleep(250);
// this does not work:
FEditorFrame.RichEdit1.Perform(WM_SETTEXT, 0, PWChar(Clipboard.AsText));
finally
InputList.Free;
end;
// at end, switch back to applicatiin
Winapi.Windows.SetForegroundWindow(self.Handle);
FEditorFrame.RichEdit1.PasteFromClipboard;
end;
end;
CloseHandle(hSnap);
end;