-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeasuringStickApp.cs
More file actions
197 lines (168 loc) · 5.29 KB
/
MeasuringStickApp.cs
File metadata and controls
197 lines (168 loc) · 5.29 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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Microsoft.Win32;
namespace MeasuringStick;
public class MeasuringStickApp : IDisposable
{
private readonly NotifyIcon _trayIcon;
private readonly ContextMenuStrip _contextMenu;
private readonly AppSettings _settings;
private MeasurementOverlay? _overlay;
private bool _disposed;
private const string AppName = "MeasuringStick";
private const string StartupKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
public MeasuringStickApp()
{
_settings = new AppSettings();
_settings.Load();
_contextMenu = new ContextMenuStrip();
_contextMenu.Items.Add("Measure", null, OnMeasureClick);
_contextMenu.Items.Add("Measure + Screenshot", null, OnMeasureScreenshotClick);
_contextMenu.Items.Add("Region Screenshot", null, OnRegionScreenshotClick);
_contextMenu.Items.Add("-");
var startupItem = new ToolStripMenuItem("Start with Windows");
startupItem.Checked = IsStartupEnabled();
startupItem.Click += OnStartupToggle;
_contextMenu.Items.Add(startupItem);
_contextMenu.Items.Add("Settings...", null, OnSettingsClick);
_contextMenu.Items.Add("-");
_contextMenu.Items.Add("Exit", null, OnExitClick);
_trayIcon = new NotifyIcon
{
Icon = CreateTrayIcon(),
Text = "Measuring Stick - Click to measure",
Visible = true,
ContextMenuStrip = _contextMenu
};
_trayIcon.Click += OnTrayIconClick;
}
private Icon CreateTrayIcon()
{
var bitmap = new Bitmap(32, 32);
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
using var pen = new Pen(Color.White, 2f);
using var brush = new SolidBrush(Color.White);
g.DrawLine(pen, 4, 16, 28, 16);
var leftArrow = new Point[]
{
new Point(4, 16),
new Point(10, 10),
new Point(10, 22)
};
g.FillPolygon(brush, leftArrow);
var rightArrow = new Point[]
{
new Point(28, 16),
new Point(22, 10),
new Point(22, 22)
};
g.FillPolygon(brush, rightArrow);
}
return Icon.FromHandle(bitmap.GetHicon());
}
private void OnTrayIconClick(object? sender, EventArgs e)
{
if (e is MouseEventArgs me && me.Button == MouseButtons.Left)
{
StartMeasurement(OverlayMode.Measure);
}
}
private void OnMeasureClick(object? sender, EventArgs e)
{
StartMeasurement(OverlayMode.Measure);
}
private void OnMeasureScreenshotClick(object? sender, EventArgs e)
{
StartMeasurement(OverlayMode.MeasureAndScreenshot);
}
private void OnRegionScreenshotClick(object? sender, EventArgs e)
{
StartMeasurement(OverlayMode.RegionScreenshot);
}
private void StartMeasurement(OverlayMode mode)
{
if (_overlay != null && !_overlay.IsDisposed)
{
_overlay.Close();
_overlay.Dispose();
}
_overlay = new MeasurementOverlay(_settings, mode);
_overlay.Show();
}
private void OnSettingsClick(object? sender, EventArgs e)
{
using var dialog = new SettingsDialog(_settings);
dialog.ShowDialog();
}
private void OnStartupToggle(object? sender, EventArgs e)
{
if (sender is ToolStripMenuItem item)
{
if (item.Checked)
{
DisableStartup();
item.Checked = false;
}
else
{
EnableStartup();
item.Checked = true;
}
}
}
private static bool IsStartupEnabled()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(StartupKey, false);
return key?.GetValue(AppName) != null;
}
catch
{
return false;
}
}
private static void EnableStartup()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(StartupKey, true);
var exePath = Application.ExecutablePath;
key?.SetValue(AppName, $"\"{exePath}\"");
}
catch (Exception ex)
{
MessageBox.Show($"Failed to enable startup: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void DisableStartup()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(StartupKey, true);
key?.DeleteValue(AppName, false);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to disable startup: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnExitClick(object? sender, EventArgs e)
{
_trayIcon.Visible = false;
Application.Exit();
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_overlay?.Dispose();
_trayIcon.Dispose();
_contextMenu.Dispose();
}
}