-
Notifications
You must be signed in to change notification settings - Fork 5
/
HighlighterRectangl.cs
206 lines (181 loc) · 7.13 KB
/
HighlighterRectangl.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Drawing;
using System.Windows.Forms;
namespace Ulavali
{
internal class HighlightRectangle
{
#region Private Fields
private bool _highlightShown;
private readonly int _highlightLineWidth;
private Rectangle _highlightLocation;
private readonly Form _leftForm;
private readonly Form _topForm;
private readonly Form _rightForm;
private readonly Form _bottomForm;
private readonly Form _infoForm;
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <remarks>
/// Creates each side of the highlight rectangle as a form, so that
/// drawing and erasing are handled automatically.
/// </remarks>
public HighlightRectangle()
{
// Construct the rectangle and set some values.
_highlightShown = false;
_highlightLineWidth = 3;
_leftForm = new Form();
_topForm = new Form();
_rightForm = new Form();
_bottomForm = new Form();
_infoForm = new Form();
Form[] forms = { _leftForm, _topForm, _rightForm, _bottomForm };
foreach (Form form in forms)
{
form.FormBorderStyle = FormBorderStyle.None;
form.ShowInTaskbar = false;
form.TopMost = true;
form.Visible = false;
form.Left = 0;
form.Top = 0;
form.Width = 1;
form.Height = 1;
form.BackColor = Color.Red;
// Make it a tool window so it doesn't show up with Alt+Tab.
int style = NativeMethods.GetWindowLong(
form.Handle, NativeMethods.GWL_EXSTYLE);
NativeMethods.SetWindowLong(
form.Handle, NativeMethods.GWL_EXSTYLE,
style | NativeMethods.WS_EX_TOOLWINDOW);
}
}
#endregion
#region Public Properties
/// <summary>
/// Sets the visible state of the rectangle.
/// </summary>
/// <remarks>
/// The Layout method is called by using BeginInvoke, to prevent
/// cross-thread updates to the UI. This method can be called on
/// any form that belongs to the UI thread.
/// </remarks>
public bool Visible
{
set
{
if (_highlightShown == value) return;
_highlightShown = value;
if (_highlightShown)
{
var mi = new MethodInvoker(Layout);
_leftForm.BeginInvoke(mi);
mi = ShowRectangle;
_leftForm.BeginInvoke(mi);
}
else
{
var mi = new MethodInvoker(HideRectangle);
_leftForm.BeginInvoke(mi);
}
}
}
/// <summary>
/// Sets the location of the highlight.
/// </summary>
public Rectangle Location
{
set
{
_highlightLocation = value;
var mi = new MethodInvoker(Layout);
_leftForm.BeginInvoke(mi);
}
get { return _highlightLocation; }
}
#endregion
#region Private Methods
/// <summary>
/// Shows or hides the rectangle.
/// </summary>
/// <param name="show">true to show, false to hide.</param>
private void Show(bool show)
{
if (show)
{
NativeMethods.ShowWindow(_leftForm.Handle, NativeMethods.SW_SHOWNA);
NativeMethods.ShowWindow(_topForm.Handle, NativeMethods.SW_SHOWNA);
NativeMethods.ShowWindow(_rightForm.Handle, NativeMethods.SW_SHOWNA);
NativeMethods.ShowWindow(_bottomForm.Handle, NativeMethods.SW_SHOWNA);
}
else
{
_leftForm.Hide();
_topForm.Hide();
_rightForm.Hide();
_bottomForm.Hide();
}
}
/// <summary>
/// Shows the highlight.
/// </summary>
/// <remarks> Parameterless method for MethodInvoker.</remarks>
void ShowRectangle()
{
Show(true);
}
/// <summary>
/// Hides the highlight.
/// </summary>
/// <remarks> Parameterless method for MethodInvoker.</remarks>
void HideRectangle()
{
Show(false);
}
/// <summary>
/// Sets the position and size of the four forms that make up the rectangle.
/// </summary>
/// <remarks>
/// Use the Win32 SetWindowPosfunction so that SWP_NOACTIVATE can be set.
/// This ensures that the windows are shown without receiving the focus.
/// </remarks>
private void Layout()
{
// Use SetWindowPos instead of changing the location via form properties:
// this allows us to also specify HWND_TOPMOST.
// Using Form.TopMost = true to do this has the side-effect
// of activating the rectangle windows, causing them to gain the focus.
NativeMethods.SetWindowPos(_leftForm.Handle, NativeMethods.HWND_TOPMOST,
_highlightLocation.Left - _highlightLineWidth,
_highlightLocation.Top,
_highlightLineWidth, _highlightLocation.Height,
NativeMethods.SWP_NOACTIVATE);
NativeMethods.SetWindowPos(_topForm.Handle, NativeMethods.HWND_TOPMOST,
_highlightLocation.Left - _highlightLineWidth,
_highlightLocation.Top - _highlightLineWidth,
_highlightLocation.Width + 2 * _highlightLineWidth,
_highlightLineWidth,
NativeMethods.SWP_NOACTIVATE);
NativeMethods.SetWindowPos(_rightForm.Handle, NativeMethods.HWND_TOPMOST,
_highlightLocation.Left + _highlightLocation.Width,
_highlightLocation.Top, _highlightLineWidth,
_highlightLocation.Height,
NativeMethods.SWP_NOACTIVATE);
NativeMethods.SetWindowPos(_bottomForm.Handle, NativeMethods.HWND_TOPMOST,
_highlightLocation.Left - _highlightLineWidth,
_highlightLocation.Top + _highlightLocation.Height,
_highlightLocation.Width + 2 * _highlightLineWidth,
_highlightLineWidth,
NativeMethods.SWP_NOACTIVATE);
NativeMethods.SetWindowPos(_infoForm.Handle, NativeMethods.HWND_TOPMOST,
_highlightLocation.Left - _highlightLineWidth,
_highlightLocation.Top + _highlightLocation.Height,
5,
5,
NativeMethods.SWP_NOACTIVATE);
}
#endregion
} // class
} // namespace