Question : Clicking a button in another application

Good day all :)

I am trying to simulate a button click on another application form, I have figured it out except for one small snag.

As the second application is mine and I have the source for it, the attached code works. However when I send "ControlCaption" as an empty string, it doesn't work - I understand why but not how to get around it. I would prefer not to make unnecessary modifications to the second application.

My question: How do I simulate a button click to a TSpeedButton which a) has no handle and b) has no caption/text only an image?

Kind Regards
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Procedure ClickButton(WindowName, WindowCaption,
  ControlName, ControlCaption: String);
Var
  hParent, hControl : HWND;
Begin
  hParent := 0;
  hControl := 0;

  hParent := FindWindow(pChar(WindowName), pChar(WindowCaption));

  Try
    BringWindowToTop(hParent);
    Application.ProcessMessages;

    hControl := FindWindowEx(hParent, 0, pChar(ControlName),
      pChar(ControlCaption));

    Try
      SendMessage(hControl, BM_CLICK, 0,0);
    Finally
      hControl := 0;
    End;
  Finally
    hParent := 0;
  End;
End;

// WindowName: The name of the form (eg: TForm1)
// WindowCaption: The caption of the form (eg: Form1)
// ControlName: The name of the control (eg: TButton)
// ControlCaption: The text/caption of the control (eg: Ok)

Procedure Button2Click(Sender: TObject);
Begin
  ClickButton('TForm1', 'Form1', 'TSpeedbutton', '');
End;

Answer : Clicking a button in another application

you cannot click on a button that has no window handle by this method.

Your only solution is to simulate a mouse click on the form, knowing the coordinate of the button

Also, I don't see the need to set your handles to 0 once used.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Procedure ClickSomewhere(WindowName, WindowCaption: PChar; X,Y:Integer);
Var
  hParent : HWND;
Begin
 hParent := FindWindow(WindowName, WindowCaption);
 if hParent<>0 Then
  Begin
   BringWindowToTop(hParent);
   Application.ProcessMessages; // not sure that is useful
   SendMessage(hParent, BM_CLICK, X,Y);
  End;
End;
Random Solutions  
 
programming4us programming4us