Click a button in another program!!

Posted on 2000-02-19
Last Modified: 2011-09-20
I've got a working exe that runs ok. What i want to do is to click a button in the exe via my own program. How do i do that ?
Question by:drnadeem
LVL 20

Expert Comment

ID: 2537793
Find the button window handle (e.g. with FindWindow(Ex)), then use PostMessage(WM_KEYDOWN, ...) and PostMessage(WM_KEYUP, ...) to simulate key presses.

Regards, Madshi.

Expert Comment

ID: 2538180
look at the source on this component how to do it exactly (or just use the component) :-) 

Regards, Zif

Author Comment

ID: 2539201
Wasn't able to download/visit to your page by double clicking it.

Accepted Solution

ahalya earned 30 total points
ID: 2563800
Here is an example form an answer I preovided earlier.  This code starts the "Network" control panel applet and clicks the "Add" button.

uses ShellAPI:

var hIns : THandle;

hIns := ShellExecute(0, nil,
            'shell32.dll,Control_RunDLL netcpl.cpl',
WaitForSingleObject(hIns, 10 *1000);

    h := FindWindow('#32770', 'Network');
    if h = 0 then sleep(10);
until h <> 0;

DlgID := 209; //for Add button
PostMessage(h, wm_Command, DlgID, MakeLong(h, BN_Clicked));


You can write a similar routine. You'll need to know the className & WindowTitle of the parent application, and the button. In pseudo-code your procedure will look like.

var h, hc : HWND;
      DlgID : integer;

h := FindWindow('ClassName', 'WindowTitle');
hc := FindWindowEx(h, 0, 'TButton', 'Button Title');
DlgID := GetDlgCtrlID(hc);
PostMessage(h, wm_Command, DlgID, MakeLong(h, BN_Clicked));

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 134
Error E2158 compiling with Delphi XE10 Seattle 2 100
Performance of SQL statement 37 111
Adoquery sql  left join does not work 25 91
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question