Code:
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
private const int BN_CLICKED = 245;
private void button1_Click(object sender, EventArgs e)
{
int hwnd = 0;
IntPtr hwndChild = IntPtr.Zero;
//Get a handle for the Application main window
hwnd = FindWindow(null, "Calculator");
//Get a handle for the buttons NAME (In the Code)
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "1");
//Send BN_CLICKED message (Means, it presses it)
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
//Get a handle for the buttons NAME (In the Code)
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "2");
//Send BN_CLICKED message (Means, it presses it)
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
}