开发者

How to make a program that checks if a different window is open, then fills in forms?

I'm trying to make a program that checks if a specific window (from a separate program) is open, then if it's open, it will fill in forms automatically in that window. I have no idea where to start, I looked it up online and couldn't find anything on the topic.

An example of this is trying to fill in login information in a separate windows for开发者_StackOverflow社区m automatically.

How would this be made? Is it even possible? If possible, I would like any answers in C#.


I would look at the the Windows Automation API and in the System.Windows.Automation namespace.


To find a window with a specific title, use interop from C# to call the Win32 function EnumWindows, which will give you the windows' characteristics and titles. You can then activate the window and call SendInput to send keystrokes.


It is definitely possible.

You'll need win32 APIs for interop (getting handles to other applications' controls and sending windows messages to them); this site is invaluable: http://pinvoke.net/

You'll need to understand controls and how to target them. Invaluable to this is Winspector Spy, but I think this product may no longer be around. Look for Winspector alternatives. I know that one comes with AutoHotkey (free), but I don't think it is as full-featured as was Winspector.

Although you can probably write some nice C# wrappers around this stuff, most of the work has to be done in the win32 APIs, not .NET framework classes/methods.


If you can introduce a dependency on WPF, look into using WPF's UI Automation framework. It's meant to be used for both automated UI testing and assistive technology like screen readers, so it would definitely work for filling out a form in another application.


AutoIt is a scripting language especially designed for this purpose

http://www.autoitscript.com/

If you really need this inside a C# program, you can use the COM interface of AutoIt.

EDIT: here is a very similar question:

What's a good, if any, .NET Windows automation library?

The answers given there should help you.


Is this for a web app or a desktop app? The answer will vary greatly depending on which one it is.

Web sites are much easier IMO todo this with - a quick perl script using curl is probably the easiest way, and a great learning experience too. You could also use C# and use the curl API, or just make C# Run() calls to execute command line calls to curl (console app) itself. The bottom line: you need to make POST/GET HTTP calls with the given info. Fiddler is your friend here - use it to find out how the website is expecting the calls and then mimic it using curl.

A windows application would require some PInvoke calls to windows message pump on the given app; e.g. C++ would be easier here depending on your experience/requirements, or you can easily wrap C++ API calls in C# using PInvoke. Either way, AFAIK there is no way to do this for a windows app in 100% managed code - I could certainly be wrong, but I've never seen it and am interested myself to find it's possible. Bottom line: you need to make calls to the win32 API to find which window it is - and then send keystrokes to that window in the appropriate places.


You will have to use Spy++ or some other tool to get the class name and windows name. They should probably be configurable, don't recommmend hard codeing the values. Here we do a loop and look for the window and then close it. I've included the appropriate imports, windows API signatures and sample code.

//DLL Imports

using System.Runtime.InteropServices;

//For Sending Keys

using System.Windows.Forms;



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

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

Sample Code:

    IntPtr handle= IntPtr.Zero;
    int retryCount = 0;

    //Try until max is reached
    while (retryCount < 30)
    {
        //Get a handle to the window  
        handle= FindWindow(WindowClassName, WindowName);

        //We found the window
        if (handle!= IntPtr.Zero)
        {
            retryCount = 30;
            //Send Close Command 
            SetForegroundWindow(handle);
            SendKeys.SendWait("%{F4}");
        }

        retryCount++;
        //Wait 2 seconds before trying again
        System.Threading.Thread.Sleep(2000);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜