-
Notifications
You must be signed in to change notification settings - Fork 5
/
LivePropertyDisplay.cs
134 lines (118 loc) · 5.95 KB
/
LivePropertyDisplay.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
using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Forms;
using Point = System.Drawing.Point;
namespace Ulavali
{
public class LivePropertyDisplay
{
private readonly ArrayList _automationObjectProperties = new ArrayList();
private readonly Form _liveDisplayForm = new Form();
private readonly DataGridView _dataGrid = new DataGridView();
private Rectangle _dispalyFormRectangle;
public LivePropertyDisplay()
{
_liveDisplayForm.FormBorderStyle = FormBorderStyle.None;
_liveDisplayForm.ShowInTaskbar = false;
_liveDisplayForm.TopMost = true;
_liveDisplayForm.Visible = false;
_liveDisplayForm.Opacity = 0.85;
_liveDisplayForm.MouseEnter += LiveDisplayFormOnMouseEnter;
_liveDisplayForm.MouseMove += LiveDisplayFormOnMouseEnter;
_dataGrid.AllowUserToDeleteRows = false;
_dataGrid.AllowUserToResizeColumns = false;
_dataGrid.AllowUserToResizeRows = false;
_dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
_dataGrid.ColumnHeadersVisible = false;
_dataGrid.EditMode = DataGridViewEditMode.EditProgrammatically;
_dataGrid.GridColor = Color.PowderBlue;
_dataGrid.Location = new Point(0, 0);
_dataGrid.MultiSelect = false;
_dataGrid.ReadOnly = true;
_dataGrid.RowHeadersVisible = false;
_dataGrid.ScrollBars = ScrollBars.None;
_dataGrid.SelectionMode = DataGridViewSelectionMode.CellSelect;
// Make it a tool window so it doesn't show up with Alt+Tab.
var style = NativeMethods.GetWindowLong(
_liveDisplayForm.Handle, NativeMethods.GWL_EXSTYLE);
NativeMethods.SetWindowLong(
_liveDisplayForm.Handle, NativeMethods.GWL_EXSTYLE,
style | NativeMethods.WS_EX_TOOLWINDOW);
_dataGrid.Dock = DockStyle.Fill;
_liveDisplayForm.Controls.Add(_dataGrid);
}
private void LiveDisplayFormOnMouseEnter(object sender, EventArgs args)
{
Hide();
}
public void Hide()
{
NativeMethods.ShowWindow(_liveDisplayForm.Handle, NativeMethods.SW_HIDE);
}
public void LoadAutomationObjectForDisplay(AutomationElement currentAutomationElement)
{
_dataGrid.DataSource = UpdateElementPropertyArray(currentAutomationElement);
Rect objectBoundingRectangle = currentAutomationElement.Current.BoundingRectangle;
_dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
_dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
_liveDisplayForm.AutoSize = false;
_liveDisplayForm.AutoSizeMode = AutoSizeMode.GrowOnly;
_liveDisplayForm.AutoSize = true;
PositionLiveDiplayDialog(objectBoundingRectangle);
}
private void PositionLiveDiplayDialog(Rect objectBoundingRectangle)
{
int totalColumnWidth = _dataGrid.Columns.Cast<DataGridViewColumn>()
.Aggregate(0, (current, column) => current + column.Width);
int totalRowHeight = _dataGrid.Rows.Cast<DataGridViewRow>()
.Aggregate(0, (current, row) => current + row.Height);
_dispalyFormRectangle = new Rectangle((int) (objectBoundingRectangle.X + objectBoundingRectangle.Width),
(int) (objectBoundingRectangle.Y + objectBoundingRectangle.Height),
totalColumnWidth,
totalRowHeight);
BringDisplayRectangleIntoScreen(objectBoundingRectangle);
NativeMethods.SetWindowPos(_liveDisplayForm.Handle, NativeMethods.HWND_TOPMOST,
_dispalyFormRectangle.X,
_dispalyFormRectangle.Y,
totalColumnWidth,
totalRowHeight,
NativeMethods.SWP_NOACTIVATE);
}
private void BringDisplayRectangleIntoScreen(Rect testElementRectangle)
{
var screenRectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
if (screenRectangle.Contains(_dispalyFormRectangle)) return;
if (screenRectangle.Width < _dispalyFormRectangle.X + _dispalyFormRectangle.Width)
{
_dispalyFormRectangle.X = screenRectangle.Width - _dispalyFormRectangle.Width;
}
if (screenRectangle.Height >= _dispalyFormRectangle.Height + _dispalyFormRectangle.Y) return;
_dispalyFormRectangle.Y = (int) (testElementRectangle.Y - _dispalyFormRectangle.Height);
if (screenRectangle.Contains(_dispalyFormRectangle)) return;
_dispalyFormRectangle.Y =
(int) (testElementRectangle.Y + testElementRectangle.Height - _dispalyFormRectangle.Height);
}
private ArrayList UpdateElementPropertyArray(AutomationElement currentAutomationElement)
{
_automationObjectProperties.Clear();
_automationObjectProperties.Add(new ObjectProperty("Name", currentAutomationElement.Current.Name));
_automationObjectProperties.Add(new ObjectProperty("Automation Id",
currentAutomationElement.Current.AutomationId));
_automationObjectProperties.Add(new ObjectProperty("Process Id",
currentAutomationElement.Current.ProcessId.ToString(CultureInfo.InvariantCulture)));
_automationObjectProperties.Add(new ObjectProperty("Control Type",
currentAutomationElement.Current.LocalizedControlType));
return _automationObjectProperties;
}
public void Show()
{
NativeMethods.ShowWindow(_liveDisplayForm.Handle, NativeMethods.SW_SHOWNA);
}
}
}