In support of my talk on Saturday, I wanted to publish another little bit of code that I have found extremely useful. There are a lot of spy utilities out there: Spy++, ManagedSpy, UISpy, etc. They all work OK, but I have found on many occasions that I wanted my spy utility to do X, Y or Z.
So, I built my own spy utility. Start with this form, and add any spy functionality that you need to it. Here is the code:
1: using System.Drawing;
2: using System.Windows.Forms;
3:
4: namespace RecipeBox.Tests.GUI_Tests
5: {
6: public class Spy : Form
7: {
8: readonly SplitContainer _splitContainer = new SplitContainer {Dock = DockStyle.Fill};
9: readonly TreeView _controlTree = new TreeView {Dock = DockStyle.Fill};
10: readonly PropertyGrid _properties = new PropertyGrid {Dock = DockStyle.Fill};
11:
12: public Spy()
13: {
14: Size = new Size(640, 480);
15: _splitContainer.Panel1.Controls.Add(_controlTree);
16: _splitContainer.Panel2.Controls.Add(_properties);
17: Controls.Add(_splitContainer);
18: _controlTree.NodeMouseClick += (sender, e) => _properties.SelectedObject = e.Node.Tag;
19: }
20:
21: public Spy(Control rootControl) : this()
22: {
23: _controlTree.Nodes.Add(GetTreeNode(rootControl));
24: }
25:
26: private static TreeNode GetTreeNode(Control control)
27: {
28: var node = new TreeNode(control + " (" + control.Name + ")") {Tag = control};
29: foreach (Control childControl in control.Controls)
30: node.Nodes.Add(GetTreeNode(childControl));
31: return node;
32: }
33: }
34: }
And here is how you might use the custom spy:
1: [Test, Explicit]
2: public void Spy_On_TestForm()
3: {
4: var testForm = new TestForm();
5: testForm.Show();
6:
7: var spy = new Spy(testForm);
8: spy.Show();
9:
10: while (true)
11: Application.DoEvents();
12: }
And this is what it looks like: