-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
162 lines (144 loc) · 5.58 KB
/
Program.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Forms;
using Ulavali.Properties;
using Size=System.Drawing.Size;
namespace Ulavali
{
internal static class Program
{
[STAThread]
private static void Main()
{
new WhiteSpy().Run();
}
}
internal class WhiteSpy
{
private const int TimeInterval = 2;
private readonly CatchObjectAtMousePoint _catchObjcetAtMouse = new CatchObjectAtMousePoint();
private readonly ToolStripMenuItem _exitToolStripMenuItem = new ToolStripMenuItem();
private readonly HighlightRectangle _highlight = new HighlightRectangle();
private readonly KeyboardHook _keyboardHook = new KeyboardHook();
private readonly LivePropertyDisplay _livePropertyDisplay = new LivePropertyDisplay();
private readonly NotifyIcon _systemTrayControl = new NotifyIcon();
private readonly ContextMenuStrip _systemTrayMenu = new ContextMenuStrip();
private readonly Timer _timer = new Timer();
private static AutomationElement _currentAutomationElement = AutomationElement.RootElement;
private static Rect _focusedRect;
private static AutomationElement _previousFocusedAutomationElement;
private static SpyState _spyState;
private enum SpyState
{
Spying, ShowProperties, Idel
} ;
public void Run()
{
Application.EnableVisualStyles();
InitilizeSystemTrayControl();
StartListeningToMouseMoves();
Application.Run();
}
private void InitilizeSystemTrayControl()
{
_systemTrayControl.Visible = true;
_systemTrayControl.Icon = new Icon("image\\User.ico");
//
// SystemTrayControl
//
_systemTrayControl.BalloonTipText = Resources.Start_Spying;
_systemTrayControl.ContextMenuStrip = _systemTrayMenu;
_systemTrayControl.Text = Resources.Start_Spying;
_systemTrayControl.Visible = true;
_systemTrayControl.Click += OnClickOnSystemTrayControl;
//
// SystemTrayMenu
//
_systemTrayMenu.Items.AddRange(new ToolStripItem[]
{
_exitToolStripMenuItem
});
_systemTrayMenu.Name = "SystemTrayMenu";
_systemTrayMenu.Size = new Size(104, 48);
//
// ExitToolStripMenuItem
//
_exitToolStripMenuItem.Name = "ExitToolStripMenuItem";
_exitToolStripMenuItem.Size = new Size(103, 22);
_exitToolStripMenuItem.Text = Resources.SystemTrayControl_exit;
_exitToolStripMenuItem.Click += ExitApplication;
//
}
private void ExitApplication(object sender, EventArgs e)
{
_systemTrayControl.Visible = false;
Application.Exit();
}
private void OnClickOnSystemTrayControl(object sender, EventArgs e1)
{
if (_systemTrayControl.Text == Resources.Start_Spying)
{
StartListeningToMouseMoves();
}
else
{
StopListening();
}
}
private void StartListeningToMouseMoves()
{
_spyState = SpyState.Spying;
_timer.Interval = TimeInterval;
_timer.Tick += HandelMouseMoveToUpdateHilight;
_timer.Start();
_keyboardHook.SetHook();
KeyboardHook.OnKeyPress += ListenKeyPress;
_systemTrayControl.Text = Resources.Stop_Spying;
}
private void StopListening()
{
_spyState = SpyState.Idel;
_timer.Stop();
KeyboardHook.OnKeyPress -= ListenKeyPress;
_highlight.Visible = false;
_livePropertyDisplay.Hide();
_systemTrayControl.Text = Resources.Start_Spying;
_keyboardHook.UnHook();
}
private void DisplayCurrentUiObjectProperties()
{
var automationElementAtStartOfThread = _currentAutomationElement;
_livePropertyDisplay.Hide();
_livePropertyDisplay.LoadAutomationObjectForDisplay(automationElementAtStartOfThread);
_livePropertyDisplay.Show();
}
private void HandelMouseMoveToUpdateHilight(object sender, EventArgs e1)
{
_previousFocusedAutomationElement = _currentAutomationElement;
_currentAutomationElement = _catchObjcetAtMouse.ObjectAtCurrentMousePosition;
if (_previousFocusedAutomationElement.Equals(_currentAutomationElement)) return;
_focusedRect = _currentAutomationElement.Current.BoundingRectangle;
_highlight.Location = new Rectangle(
(int)_focusedRect.Left, (int)_focusedRect.Top,
(int)_focusedRect.Width, (int)_focusedRect.Height);
_highlight.Visible = true;
DisplayCurrentUiObjectProperties();
}
private void ListenKeyPress(int keyChar)
{
if (keyChar != 27) return;
switch (_spyState)
{
case SpyState.Spying:
_timer.Stop();
_spyState = SpyState.ShowProperties;
break;
case SpyState.ShowProperties:
StopListening();
break;
}
}
}
}