-1

I have read this article how to add a button to another application. When the Button is added to the parent application, everything seems OK, but when this Button is added to another app called Labform (TLabForm), the code after click is not executed. I created also a descendant to implement simple behavior after click, but no success:

  TButton2 = class (TButton)
  public
    procedure Click; override;
  end;


procedure TButton2.Click;
begin
inherited;
MessageBox(ParentWindow, 'Hello', 'Window', MB_OK);
end;


procedure TForm1.btn1Click(Sender: TObject);
var
  Button2 : TButton2 ;
  Hand: THandle;
begin
   //  Hand:= FindWindow('TLabForm', 'Labform');   // button added, but SHOWS NO message after click
   Hand:= FindWindow('TForm1', 'Form1'); // button added, and SHOWS message after click
   if Hand <> 0 then
   begin
   Button2 := TButton2.Create(self);
   Button2.ParentWindow := hand;
   Button2.BringToFront;
   end
   else
   ShowMessage('handle not found');
end;

How to solve it?

thanx

Community
  • 1
  • 1
lyborko
  • 2,571
  • 3
  • 26
  • 54

1 Answers1

1

Whilst it is technically possible to do what you want, it is excruciatingly difficult. Raymond Chen wrote about this at some length. The executive summary:

Is it technically legal to have a parent/child or owner/owned relationship between windows from different processes? Yes, it is technically legal. It is also technically legal to juggle chainsaws.

So, you are attempting something with difficulty akin to juggling chainsaws. Unless you have a deep understanding of Win32 you've got no chance of succeeding.

So, if you want to modify the GUI of an existing process, and it's not tractable to do so with code in a different process, what can you do? Well, it follows that you need to execute code inside the target process.

That's easy enough to do with DLL injection. Inject a DLL into the process and modify it's UI from that DLL. Still not trivial. You'll have the best chance of success if you subclass a window by replacing the existing window procedure with one of your own. That will allow you to run your UI modification code in the UI thread.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490