Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Calculator through some other application in C#.net 2.0.

Author: DevAsp Team
Download Source Code : 690_ExternalCalculatorApp.zip

In this simple article you will learn how you can interact with calculator through some other application.

Simply just create New Project of C#.

 

Firstly Add Reference of System.Runtime.InteropServices.

 

using System.Runtime.InteropServices;

 

Now drag twelve Buttons on the form.

 

First there is concept to interact with any application directly, if we have Handle of any window we can do any thing with that window. Windows (Operating System) do all working through Messages. So in this article we use same technique to interact with other window.

 

We get handle of any window by giving it Class Name and its Text, we take here example of Notepad. Class name of Notepad is Notepad and you can see its text on top of window.

 

Firstly import these DLLs

 

[DllImport("user32.dll")]

public static extern int FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("user32.dll")]

public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

 

private static extern int GetParent(int hWnd);

[DllImport("user32.dll", SetLastError = true)]

 

private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

 

[DllImport("USER32.DLL")]

private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

 

FindWindow(… method return you handle of the window………

 

SendMessage(… is use to send message to Specific window……

 

GetParent(... is use to find parent of the window

 

FindWindowEx( … is use to fine window handle through only with its text

 

GetWindowLong(… use to get window properties

 

Define some global Variables these are Predefined Message of Windows (OS)

private const int WM_COMMAND = 0x0112;

        private const int WM_CLOSE = 0xF060;

        private const int BN_CLICKED = 245;

        private const int GWL_ID = -12;

        IntPtr hwndChild = IntPtr.Zero;

        IntPtr hwnd = IntPtr.Zero;

 

Now just write code on Form Load Event

 

private void Form1_Load(object sender, EventArgs e)

        {

            System.Diagnostics.Process.Start("Calc");

            System.Threading.Thread.Sleep(5000);

            hwnd = (IntPtr)FindWindow(null, "Calculator");

 

            this.Text = "DevAsp Calculator Controller";

            

          

        }

 

Now write this very simple code on Button Click Event

 

private void button1_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "1");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "2");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "3");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "4");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "5");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "6");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button12_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "8");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button11_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "9");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button8_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "0");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button10_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "+");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button9_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "-");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "X");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button13_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "=");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button14_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "%");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button15_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "/");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }

 

        private void button16_Click(object sender, EventArgs e)

        {

            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "Backspace");

            SendMessage((int)hwndChild, BN_CLICKED, 0, (int)IntPtr.Zero);

        }        }

 

 

Now your application is able to interact with calculator.

Article Comments
This is great code, but I am trying to send the same message to a sample applicationthat I put together that has just one button control.
Title of the button control i "1"
IntPtr h = Win32.FindWindowEx(hWnd, 0, "BUTTON", "1");
But this is not finding the control. Any hel p will be greatly appreciated.

Posted on 8/27/2007 7:03:33 PM by Jijo

Hi... I am a beginner in C#.Net.. So i want to learn more.. If anybody help me .. plz inform me.

Posted on 9/23/2007 2:24:38 AM by afthab

hi , great article !!
i tried this tutorial but got stuck at a point because my feeble brain can not figure out how do i use FindWindowEx function to find a dropdown list on a form ! :( can u help ?

Posted on 10/30/2007 7:57:13 AM by mansoor

please coding sent me calculator included code addittion,subscription,divide,multi,float digit,etc.

Posted on 1/26/2008 1:56:34 AM by ranu jain

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Move Window from other window without directly interacting with it in C#.net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net