A library for remote controlling other applications in C#.I wrote this library mainly to avoid having to use the p-invokes each time I need simple tasks like hiding a window or sending keystrokes to another applications. Over the time the library grew having the possibility to read textboxes and some other controls in other applications. Some controls can also be updated, but this is still some sort of alpha-feature, because as you may be aware of writing text in other application contros implies actually changing the memory of the other process and sending a message to the application with the pointer to the changed memory. This is not difficult to do, but can be time-consuming when it comes to be abstracted in a library, as sometimes this way works, sometimes you can send directly a pointer to the address-space of your application. I still didn't figure out how windows sorts this out. That being said, the library is pretty useful if you have to do simple things and it shouldn't be difficult to expand if you need some extra property not already encapsulated there.As I need more features I will be adding them to the library and posting new versions here. You're welcome to contribute if you add anything to it. Some Examples1. Manipulating windows
Check if foreground windows is Firefox and hide it if true (this could be some sort of panic button handler) ForeignWindow fore = ForeignWindow.ForegroundWindow; Close an empty tab in Firefox (I needed this in an event handler when calling a mime application, to avoid Firefox spawning a new tab each time the event is generated)
foreach (ForeignWindow wnd in ForeignWindow.TopMostWindows) Following example closes the active document in Foxit. This is done with the command 57602. How does one come to the number 57602? You can use Spy++ of Visual Studio and click on the menu yourself, then Spy++ shows in its window which command you just triggered. This way you can automatize almost everything which can be done through the application menus. foreach (ForeignWindow wnd in ForeignWindow.TopMostWindows) 2. Manipulating controls (pdf24)
pdf24 (in case you don't know it yet) is a freeware tool which allows you to create pdf's by adding a virtual printer, everything you send to the printer is converted to pdf This example encapsulates the task of creating a pdf by controlling the pdf24 window. You just call the function with the filename and the pdf is stored there. First we look during 25 seconds for the window of pdf24: ForeignWindow pdf24 = ForeignWindow.FindWindow("PDF24", "PDF24Frontend", 250, false); Now we wait until the save button becomes enabled (normally is disabled for some seconds until pdf24 feels like the file can be stored) ForeignButton btn = new ForeignButton(pdf24.FirstChild, 0x68); // Search for child with id 0x68 The button is enabled → press it: btn.Press();
After pressing the button a "Save as ..." dialog appears, search it: ForeignWindow saveAsDlg = ForeignWindow.FindWindow("Save As", "#32770", 50); And now we can just set the text and press the ok button: pdf24 informs us now that the file was saved successfully, we have to close this dialog: ForeignWindow confDlg = ForeignWindow.FindWindow("pdf24 Info", "#32770", 50); After closing the main pdf24 dialog we are done: pdf24.Close(); 3. Taking snapshots
From version 0.2 there is a very easy way to take a snapshot of any window (even background windows). Just call the function SnapShot of the class ForeignWindow. By using this function is trivial to make an application which zooms another window interactively. Next example shows how (it needs a form with a panel docked to the client area and a timer with an interval of 50 ms).
For some reason this program won't work to copy the contents of the program manager (the desktop), because it causes the whole desktop to repaint itself. If anyone knows why I would be very thankful for a hint. Download
To download the library (with source-code) go to the download page. |