-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoClickOverlayForm.cs
More file actions
307 lines (269 loc) · 11.8 KB
/
AutoClickOverlayForm.cs
File metadata and controls
307 lines (269 loc) · 11.8 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace NaturalCommands
{
/// <summary>
/// Non-intrusive overlay form that displays a blue circular countdown indicator
/// for the auto-click feature. Shows progress and remaining time near the cursor.
/// </summary>
public class AutoClickOverlayForm : Form
{
private static AutoClickOverlayForm? _instance;
private static readonly object _lock = new object();
private static System.Threading.SynchronizationContext? _uiContext;
private float _percentage = 0;
private int _remainingMs = 0;
// Track last-painted state to avoid unnecessary repaints and reduce flicker
private float _lastPaintedPercentage = -1f;
private System.Drawing.Point _lastLocation = System.Drawing.Point.Empty;
private const float _minUpdateDelta = 1f; // percent
// Cycle/animation state: switch progress color each countdown cycle and alternate a background color when a cycle completes
private static int _cycleIndex = 0;
private Color _currentProgressColor = Color.FromArgb(230, 0, 200, 0); // default bright green
private Color _currentBackgroundFill = Color.FromArgb(0, 0, 0, 0); // transparent by default
private static readonly Color[] _progressColors = new[] {
Color.FromArgb(230, 0, 200, 0), // green
Color.FromArgb(230, 255, 165, 0), // orange
Color.FromArgb(230, 0, 120, 215) // blue
};
private static readonly Color[] _backgroundCycleColors = new[] {
Color.FromArgb(120, 0, 200, 0), // semi-transparent green
Color.FromArgb(120, 255, 165, 0), // semi-transparent orange
Color.FromArgb(120, 0, 120, 215) // semi-transparent blue
};
// Win32 API to make form click-through
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private const int GWL_EXSTYLE = -20;
private const int WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private AutoClickOverlayForm()
{
// Form properties for transparent, topmost overlay
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
BackColor = Color.Lime; // Transparency key color
TransparencyKey = Color.Lime;
StartPosition = FormStartPosition.Manual;
// Small circular overlay sized to replace the system cursor while counting down
Width = 48;
Height = 48;
// Double buffering for smooth rendering
DoubleBuffered = true;
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer, true);
// Make form click-through (mouse events pass through)
Load += (s, e) =>
{
int exStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, exStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
};
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Use BufferedGraphics to reduce flicker
var context = BufferedGraphicsManager.Current;
using (var buffer = context.Allocate(e.Graphics, this.ClientRectangle))
{
var bg = buffer.Graphics;
bg.SmoothingMode = SmoothingMode.AntiAlias;
bg.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// Clear buffer with the form BackColor (the transparency key) to ensure per-pixel transparency
// This avoids leftover black pixels when using BufferedGraphics + TransparencyKey.
bg.Clear(this.BackColor);
bg.CompositingMode = CompositingMode.SourceOver;
// Calculate center and radius
int centerX = Width / 2;
int centerY = Height / 2;
int radius = Math.Min(Width, Height) / 2 - 5;
// Draw outer ring (transparent center) with a neutral dim color so progress stands out
using (var ringPen = new Pen(Color.FromArgb(140, 40, 40, 40), 3))
{
bg.DrawEllipse(ringPen, centerX - radius + 3, centerY - radius + 3, (radius - 3) * 2, (radius - 3) * 2);
}
// If the cycle has completed (full) draw a subtle filled background to indicate completion
if (_percentage >= 100 && _currentBackgroundFill.A > 0)
{
using (var fillBrush = new SolidBrush(_currentBackgroundFill))
{
bg.FillEllipse(fillBrush, centerX - radius + 6, centerY - radius + 6, (radius - 6) * 2, (radius - 6) * 2);
}
}
// Draw progress arc (bright accent) to clearly highlight progress
if (_percentage > 0)
{
using (var progressPen = new Pen(_currentProgressColor, 4))
{
float sweepAngle = (_percentage / 100f) * 360f;
bg.DrawArc(progressPen,
centerX - radius + 3,
centerY - radius + 3,
(radius - 3) * 2,
(radius - 3) * 2,
-90,
sweepAngle);
}
}
buffer.Render(e.Graphics);
}
}
/// <summary>
/// Initialize the UI context - must be called from the UI thread before using the overlay.
/// </summary>
public static void InitializeUIContext()
{
_uiContext = System.Threading.SynchronizationContext.Current;
}
/// <summary>
/// Shows or updates the overlay with current countdown information.
/// </summary>
public static void UpdateOverlay(Point cursorPos, int remainingMs, float percentage)
{
// Check if we need to invoke on UI thread
if (_uiContext != null && System.Threading.SynchronizationContext.Current != _uiContext)
{
_uiContext.Post(_ => UpdateOverlay(cursorPos, remainingMs, percentage), null);
return;
}
lock (_lock)
{
try
{
if (_instance == null || _instance.IsDisposed)
{
_instance = new AutoClickOverlayForm();
_instance.Show();
}
// Update position and values - overlay is anchored at the specified point
_instance.Location = new Point(cursorPos.X - _instance.Width / 2, cursorPos.Y - _instance.Height / 2);
_instance._remainingMs = remainingMs;
// Detect start of a new countdown cycle (percentage has moved from 0 to >0)
bool startingCycle = _instance._percentage <= 0 && percentage > 0;
_instance._percentage = percentage;
if (startingCycle)
{
_instance.StartNewCycle();
}
// Keep system cursor visible so it can move independently of the overlay
if (!_instance.Visible)
{
_instance.Show();
}
if (!_instance.TopMost)
{
_instance.TopMost = true;
}
// Only invalidate if percentage or location changed enough to avoid excess repaints (reduces flicker)
if (Math.Abs(_instance._percentage - _instance._lastPaintedPercentage) < _minUpdateDelta && _instance.Location == _instance._lastLocation)
{
}
else
{
_instance.Invalidate();
_instance._lastPaintedPercentage = _instance._percentage;
_instance._lastLocation = _instance.Location;
}
}
catch (Exception ex)
{
NaturalCommands.Helpers.Logger.LogError($"[Overlay] ERROR updating overlay: {ex.Message}\n{ex.StackTrace}");
}
}
}
/// <summary>
/// Called when a new countdown cycle starts so the overlay can change colors between cycles.
/// </summary>
private void StartNewCycle()
{
try
{
_cycleIndex = (_cycleIndex + 1) % _progressColors.Length;
_currentProgressColor = _progressColors[_cycleIndex];
// Choose the background color for when this cycle completes
_currentBackgroundFill = _backgroundCycleColors[_cycleIndex];
}
catch (Exception ex)
{
NaturalCommands.Helpers.Logger.LogError($"[Overlay] StartNewCycle error: {ex.Message}");
}
}
/// <summary>
/// Hides the overlay.
/// </summary>
public static void HideOverlay()
{
// Check if we need to invoke on UI thread
if (_uiContext != null && System.Threading.SynchronizationContext.Current != _uiContext)
{
_uiContext.Post(_ => HideOverlay(), null);
return;
}
lock (_lock)
{
if (_instance != null && !_instance.IsDisposed)
{
try
{
_instance.Hide();
}
catch (Exception ex)
{
NaturalCommands.Helpers.Logger.LogError($"Error hiding overlay: {ex.Message}");
}
}
}
}
/// <summary>
/// Completely closes and disposes the overlay.
/// </summary>
public static void CloseOverlay()
{
// Check if we need to invoke on UI thread
if (_uiContext != null && System.Threading.SynchronizationContext.Current != _uiContext)
{
_uiContext.Post(_ => CloseOverlay(), null);
return;
}
lock (_lock)
{
if (_instance != null && !_instance.IsDisposed)
{
try
{
_instance.Close();
_instance.Dispose();
}
catch (Exception ex)
{
NaturalCommands.Helpers.Logger.LogError($"Error closing overlay: {ex.Message}");
}
finally
{
_instance = null;
}
}
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
return cp;
}
}
}
}