Click here to Skip to main content
11,479,486 members (44,567 online)
Click here to Skip to main content

Using Windows APIs from C#, again!

, 20 Jun 2006 CPOL 86.6K 6.9K 44
Rate this:
Please Sign up or sign in to vote.
How to trigger events for controls on another window running in another process.

Sample Image

Introduction

This article shows how to control other windows and trigger events for their controls using Windows APIs.

In this sample, I simply get a handle for the Calculator window using the FindWindow API, get a handle for the Calculator buttons using FindWindowEx, and trigger the Button Click event for any required buttons, using the SendMessage API.

Background

The main idea I want to demonstrate here is that any form/dialog in Windows must have a window handle; with this handle and using the windows related APIs, you can control the form/dialog and trigger events for its controls.

Here's the code. I think it is well commented and needs no more explanation:

int hwnd=0;
IntPtr hwndChild=IntPtr.Zero;

//Get a handle for the Calculator Application main window
hwnd=FindWindow(null,"Calculator");
if(hwnd == 0)
{
    if(MessageBox.Show("Couldn't find the calculator" + 
                       " application. Do you want to start it?", 
                       "TestWinAPI", 
                       MessageBoxButtons.YesNo)== DialogResult.Yes)
    {
        System.Diagnostics.Process.Start("Calc");
    }
}
else
{
        
    //Get a handle for the "1" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","1");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "+" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","+");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "2" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","2");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "=" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","=");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

}

Points of interest

I think controlling windows in other processes can be very helpful in many situations like:

  1. Ensuring that a main application is running before starting a dependant application.
  2. Automating some tasks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rami Helmy
Software Developer (Senior) HP Enterprise Services (Formerly named EDS)
Egypt Egypt
I'm a senior software engineer experienced in C/C++ programming under different platforms. I like to get acquainted with new technologies that appear so fast day after day like the .NET framework, SQL 2008, etc.
Programming is both my profession and my main hobby.

I enjoy tickling with code using different programming languages on different platforms.

Comments and Discussions

 
QuestionHelp using this tool Pin
Member 109950716-Aug-14 1:22
memberMember 109950716-Aug-14 1:22 
AnswerRe: Help using this tool Pin
Rami Helmy26-Jan-15 13:09
memberRami Helmy26-Jan-15 13:09 
GeneralMy vote of 5 Pin
ProgramFOX22-Dec-12 4:14
memberProgramFOX22-Dec-12 4:14 
GeneralRe: My vote of 5 Pin
Rami Helmy10-Feb-13 22:28
memberRami Helmy10-Feb-13 22:28 
GeneralMy vote of 5 Pin
Keith Barrow6-May-12 10:15
mentorKeith Barrow6-May-12 10:15 
GeneralRe: My vote of 5 Pin
Rami Helmy10-Feb-13 22:29
memberRami Helmy10-Feb-13 22:29 
GeneralFinding the title Pin
Sam Shiles28-Aug-09 5:13
memberSam Shiles28-Aug-09 5:13 
GeneralHelp reqd for adding a button to a different app Pin
aditya.jk@gmail.com19-Feb-09 11:24
memberaditya.jk@gmail.com19-Feb-09 11:24 
GeneralRe: Help reqd for adding a button to a different app Pin
Rami Helmy6-Apr-09 10:44
memberRami Helmy6-Apr-09 10:44 
GeneralRe: Help reqd for adding a button to a different app Pin
Rami Helmy18-Apr-09 2:37
memberRami Helmy18-Apr-09 2:37 
GeneralHelp reqd for adding a button to a different app Pin
aditya.jk@gmail.com19-Feb-09 11:23
memberaditya.jk@gmail.com19-Feb-09 11:23 
GeneralControl WindowsForm application from a console application Pin
Talinu30-Oct-07 10:51
memberTalinu30-Oct-07 10:51 
GeneralC# .Net - handle issue Pin
sounakbanerjee23-Jan-07 2:48
membersounakbanerjee23-Jan-07 2:48 
GeneralRe: C# .Net - handle issue Pin
Michael Sterk23-Jan-07 2:56
memberMichael Sterk23-Jan-07 2:56 
GeneralMultiple Instances Pin
MrEyes21-Jun-06 2:28
memberMrEyes21-Jun-06 2:28 
GeneralRe: Multiple Instances Pin
Rami Helmy25-Jun-06 2:41
memberRami Helmy25-Jun-06 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by Rami Helmy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid