Related links


Visitors since 18st Feb 2009:
free counters
Free Visitor Maps at VisitorMap.org

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 Examples

1. 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;
if (fore.Caption.Contains("Mozilla Firefox"))
{
fore.ShowWindow(WindowShowStyle.Hide);
}

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)
{
if (wnd.Visible && wnd.Parent == null && wnd.Caption == "Mozilla Firefox")
{
if (wnd.FirstChild != null)
wnd.FirstChild.SimulateCtrlKey(VirtualKeys.W);
break;
}
}

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)
{
if (wnd.Visible)
{
string name = wnd.Caption;
if (name.ToLower().Contains("foxit") && name.ToLower().Contains(filename.ToLower()))
{
wnd.SendCommand(57602);
return wnd;
}
}
}

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);
if (pdf24 == null)
return false; // After 25 seconds still no window -> error

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
btn.Enabled = true;
int retryCount = 200;
while (retryCount > 0 && !btn.Enabled) // Wait until button is enabled
{
Thread.Sleep(100);
retryCount--;
}
 
if (!btn.Enabled)
return false; // Button still disabled -> error
 

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);
if (saveAsDlg == null) return false;

And now we  can just set the text and press the ok button:

ForeignButton okBtn = new ForeignButton(saveAsDlg, 1);
ForeignComboBox txtFilename = new ForeignComboBox(saveAsDlg, 0x47c);
txtFilename.Text = m_filename;
okBtn.Press();

pdf24 informs us now that the file was saved successfully, we have to close this dialog:

ForeignWindow confDlg = ForeignWindow.FindWindow("pdf24 Info", "#32770", 50);
if (confDlg == null) return false;
okBtn = new ForeignButton(confDlg, 2);
okBtn.Press();

After closing the main pdf24 dialog we are done:

pdf24.Close();
return true;

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).

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ForeignWindow vs = null;  // Window to zoom
bool bZoom = false; // whether zoom is active or not   private void Form1_Shown(object sender, EventArgs e) { foreach (ForeignWindow w in ForeignWindow.TopMostWindows) { if (w.Caption.Contains("Visual Studio")) // Search the visual studio window
 {
vs = w; break; } } }   private void timer1_Tick(object sender, EventArgs e) { if (bZoom) { Bitmap bmp = vs.Snapshot(); // Take a snapshot if (bmp != null) { Graphics g = Graphics.FromHwnd(panel1.Handle); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, panel1.ClientRectangle); // Draw it to the panel (stretching it to the size of the panel) bmp.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); } } }   private void panel1_Click(object sender, EventArgs e) { bZoom = !bZoom; // Click on the form activates or deactivates the zoom functionality }  

 

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.

 

Add your comment

Your name:
Subject:
Comment: