2

I'm trying to create a program that will be able to control another program (in Windows).

I found this code:

// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
                                       string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

//button event
private void button1_Click(object sender, EventArgs e)
{
    // Get a handle to the Calculator application. The window class 
    // and window name were obtained using the Spy++ tool.
    IntPtr calculatorHandle = FindWindow("CalcFrame", "Kalkulačka");

    // Verify that Calculator is a running process. 
    if (calculatorHandle == IntPtr.Zero)
    {
        MessageBox.Show("Calculator is not running.");
        return;
    }

    // Make Calculator the foreground application and send it  
    // a set of calculations.
    SetForegroundWindow(calculatorHandle);
    SendKeys.SendWait("111");
    SendKeys.SendWait("*");
    SendKeys.SendWait("11");
    SendKeys.SendWait("=");
}

Is is possible to simulate CLICK on button? How? It is possible to click on program in the background?

Can you show me an example ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
phawresl
  • 95
  • 1
  • 1
  • 5
  • yes this is possible I believe this may lend you some ideas -http://stackoverflow.com/questions/26502035/perform-a-mouse-click-event-on-another-application-using-c-sharp if not then perform a simple google search – MethodMan Feb 18 '15 at 17:31
  • possible duplicate of [Control another application using C#](http://stackoverflow.com/questions/1134993/control-another-application-using-c-sharp) – Scott Solmer Feb 18 '15 at 17:51
  • Yes, i read this, but i dont know how to use library. I think is there a another way. – phawresl Feb 18 '15 at 17:52

2 Answers2

1

You may find the answer in other posts:

programmatically mouse click in another window

or

Send mouse clicks to X Y coordinate of another application

I hope they help.

Community
  • 1
  • 1
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
1

You can use the following code to simulate mouse click:

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public const int MOUSE_LEFTDOWN = 0x02;
        public const int MOUSE_LEFTUP = 0x04;

        public static void LeftMouseClick(int x, int y)
        {
            SetCursorPos(x, y);
            mouse_event(MOUSE_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSE_LEFTUP, x, y, 0, 0);
        }

Method LeftMouseClick is getting two parameters x and y representing coordinates on user screen:

LeftMouseClick(400, 200);

Or you can do it by keyboard: Link

private void button2_Click(object sender, EventArgs e)
    {          
       SendKeys.Send("{ENTER}");
    } 

basically that's what you are doing in your code:

SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");

I dont think there is another way of doing this.

Dn24Z
  • 149
  • 5
  • @phawres You can send keystrokes to the active application, that is the closest you can get to "looking" as a backround operation, see edit above. – Dn24Z Feb 18 '15 at 18:20
  • @phawres you can only do it in background by accessing some sort of powershell, application-programming interface (API), macro or dll. If an aplication dosnt support that type of access you can not do it programmatically or as a backround operation. – Dn24Z Feb 18 '15 at 18:40