Examples Delphi

Title: Get/Set a Text (Memo) from another application.
Question: Did you face a situation where you need to copy/paste some text from another application to your application (we are not talking about OLE)?
Answer:
Here is the code:
Function GetOtherWindowMemoText(const sCaption : String) : WideString;
var
hWindow : THandle;
hChild : THandle;
aTemp : array[0..5000] of Char;
sClassName : String;
begin
Result := '';
hWindow := FindWindow(Nil,PChar(sCaption));
if hWindow = 0 then begin
ShowMessage('Could NOT find the other program');
exit;
end;
hChild := GetWindow(hWindow, GW_CHILD);
while hChild 0 do Begin
if GetClassName(hChild, aTemp, SizeOf(aTemp)) 0 then begin
sClassName := StrPAS(aTemp);
if sClassName = 'Edit' then begin
SendMessage(hChild,WM_GETTEXT,SizeOf(aTemp),Integer(@aTemp));
Result := StrPAS(aTemp);
end;
end;
hChild := GetWindow(hChild, GW_HWNDNEXT);
end;
end;
Function SetOtherWindowMemoText(const sCaption : String; const sText : String) : WideString;
var
hWindow : THandle;
hChild : THandle;
aTemp : array[0..5000] of Char;
sClassName : String;
begin
Result := '';
hWindow := FindWindow(Nil,PChar(sCaption));
if hWindow = 0 then begin
ShowMessage('Could NOT find the other program');
exit;
end;
hChild := GetWindow(hWindow, GW_CHILD);
while hChild 0 do Begin
if GetClassName(hChild, aTemp, SizeOf(aTemp)) 0 then begin
sClassName := StrPAS(aTemp);
if sClassName = 'Edit' then begin
StrPCopy(aTemp,sText);
SendMessage(hChild,WM_SETTEXT,SizeOf(aTemp),Integer(@aTemp));
end;
end;
hChild := GetWindow(hChild, GW_HWNDNEXT);
end;
end;
Let say you want to get the text from Notepad, then you should pass the title of notepad to the previous function and assign the return value to Rich edit. An example:
...............................................
//Get text from noteppad.
RichEdit1.Lines.Text := GetOtherWindowMemoText('Untitled - Notepad');
...............................................
//Set notepad memo text.
SetOtherWindowMemoText('Untitled - Notepad',RichEdit1.Lines.Text);
...............................................
Regards,
Abdulaziz Jasser