Button click vb.net


Click here to Skip to main content

11,415,734 members (84,844 online)

Click here to Skip to main content

Using Windows APIs from C#, again!

, 20 Jun 2006 CPOL

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 theSendMessage 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

You must Sign In to use this message board.
Search Comments  
Profile popups    Spacing
Relaxed
Compact
Tight
  Noise
Very High
High
Medium
Low
Very Low
  Layout
Normal
Open Topics
Open All
Thread View
  Per page
10
25
50
   

Question Help using this tool Pin member Member 10995071 6-Aug-14 1:22
Answer Re: Help using this tool Pin member Rami Helmy 26-Jan-15 13:09
General My vote of 5 Pin member ProgramFOX 22-Dec-12 4:14
General Re: My vote of 5 Pin member Rami Helmy 10-Feb-13 22:28
General My vote of 5 Pin mentor Keith Barrow 6-May-12 10:15
General Re: My vote of 5 Pin member Rami Helmy 10-Feb-13 22:29
General Finding the title Pin member Sam Shiles 28-Aug-09 5:13
General Help reqd for adding a button to a different app Pin member aditya.jk@gmail.com 19-Feb-09 11:24
General Re: Help reqd for adding a button to a different app Pin member Rami Helmy 6-Apr-09 10:44
General Re: Help reqd for adding a button to a different app Pin member Rami Helmy 18-Apr-09 2:37
General Help reqd for adding a button to a different app Pin member aditya.jk@gmail.com 19-Feb-09 11:23
General Control WindowsForm application from a console application Pin member Talinu 30-Oct-07 10:51
General C# .Net – handle issue Pin member sounakbanerjee 23-Jan-07 2:48
General Re: C# .Net – handle issue Pin member Michael Sterk 23-Jan-07 2:56
General Multiple Instances Pin member MrEyes 21-Jun-06 2:28
General Re: Multiple Instances Pin member Rami Helmy 25-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.

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

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: