We help IT Professionals succeed at work.
Get Started

Clicking a button in another application

DeonM
DeonM asked
on
8,603 Views
Last Modified: 2012-05-09
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
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;

Open in new window

Comment
Watch Question
Freelance Project Manager
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE