Vitoc, the code you requested

Started by Gardner Denver, Mar 23, 2008, 12:51 PM

Previous topic - Next topic

Gardner Denver

// Get a handle to an application window.
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName,
    string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
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("SciCalc", "Calculator");

    // 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("=");
}
****
* Gardners Note
****
The title in FindWindow("xxx") must match the windown name exactly
So if you were going to take controll of Megamud, it would look for, in my case,
FindWindow("Megamud v1.03u-[Gardner Entconn]")  Sending the lpClassName is optional.

The Sendkeys command can then send any combination of keystrokes and megamud will think the user is actually typing them.

The .sendwait option is supposed to make your application wait until the keystrokes have been processed but I've run into some issues with that (especially with Megamud) so I usually program in a small delay between lines of about 2 to 5 seconds to allow Megamud to process the information being sent and skip the .sendwait option.