Click here to Skip to main content
Licence CPOL
First Posted 20 Jun 2006
Views 48,551
Bookmarked 39 times

Using Windows APIs from C#, again!

By | 20 Jun 2006 | Article
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)

About the Author

Rami Helmy

Software Developer (Senior)
HP Enterprise Services (Formerly named EDS)
Egypt Egypt

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmentorKeith Barrow9:15 6 May '12  
GeneralFinding the title PinmemberSam Shiles4:13 28 Aug '09  
GeneralHelp reqd for adding a button to a different app Pinmemberaditya.jk@gmail.com10:24 19 Feb '09  
GeneralRe: Help reqd for adding a button to a different app PinmemberRami Helmy9:44 6 Apr '09  
GeneralRe: Help reqd for adding a button to a different app PinmemberRami Helmy1:37 18 Apr '09  
GeneralHelp reqd for adding a button to a different app Pinmemberaditya.jk@gmail.com10:23 19 Feb '09  
GeneralInvoking on Remote Machine Pinmemberkpb6:27 11 Aug '08  
GeneralControl WindowsForm application from a console application PinmemberTalinu9:51 30 Oct '07  
GeneralC# .Net - handle issue Pinmembersounakbanerjee1:48 23 Jan '07  
GeneralRe: C# .Net - handle issue PinmemberMichael Sterk1:56 23 Jan '07  
GeneralMultiple Instances PinmemberMrEyes1:28 21 Jun '06  
GeneralRe: Multiple Instances PinmemberRami Helmy1:41 25 Jun '06  

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.

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