-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
177 lines (149 loc) · 5.7 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
namespace TrackPrintScreen
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
NotifyIcon trayIcon;
OptionsWindow optionsWindow = null;
Screenshotter screenshotter = null;
System.Windows.Threading.DispatcherTimer dispatcherTimer = null;
Options options;
string configPath = "config.txt";
public MainWindow()
{
InitializeComponent();
WindowState = WindowState.Minimized;
Hide();
if (!System.IO.File.Exists(configPath))
{
System.IO.File.WriteAllText(configPath, System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));
}
options = OptionsFiledatabase.Load(configPath);
while (!System.IO.Directory.Exists(options.FolderPath) && revertToThisFolderDialog() == false) ;
if (!System.IO.Directory.Exists(options.FolderPath))
{
System.IO.File.WriteAllText(configPath, System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));
options = OptionsFiledatabase.Load(configPath);
}
screenshotter = new Screenshotter(options);
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = new System.Drawing.Icon("icon.ico"), //Resources.AppIcon,
Visible = true,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Shot", SpecialShot),
new MenuItem("Open Screenshot Folder", OpenScreenShotFolder),
new MenuItem("Options", OpenOptions),
new MenuItem("Exit", Exit)
})
};
trayIcon.DoubleClick += OpenOptions;
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 1, 0);
dispatcherTimer.Start();
screenshotter.Shot(); // initial shot at the start of the application
verifyDiskSpace();
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
if(optionsWindow != null)
{
optionsWindow.Close();
}
Environment.Exit(0);
}
void OpenOptions(object sender, EventArgs e)
{
if (optionsWindow != null) return;
optionsWindow = new OptionsWindow(options);
optionsWindow.Closed += OptionsWindowClosed;
optionsWindow.Show();
}
void OptionsWindowClosed(object sender, EventArgs e)
{
options = optionsWindow.getOptions();
OptionsFiledatabase.Save(configPath, options);
optionsWindow = null;
screenshotter.UpdateOptions(options);
}
void SpecialShot(object sender, EventArgs e)
{
var timer = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromMilliseconds(400) };
timer.Start();
timer.Tick += (s, a) =>
{
timer.Stop();
screenshotter.Shot(true);
};
}
void OpenScreenShotFolder(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", options.FolderPath);
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
screenshotter.Shot();
}
private void verifyDiskSpace()
{
FileInfo f = new FileInfo(options.FolderPath);
string drive = System.IO.Path.GetPathRoot(f.FullName);
var freespace = GetTotalFreeSpace(drive);
if(freespace < 1024 * 1024 * 100)
{
System.Windows.Forms.MessageBox.Show("Tracking print screen space is lower than 100MB.");
}
}
private long GetTotalFreeSpace(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
{
return drive.TotalFreeSpace;
}
}
return -1;
}
private bool revertToThisFolderDialog()
{
string sMessageBoxText = "Couldn't save the picture in the setting's folder. Want to revert to default?";
string sCaption = "Screenshotter";
MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;
MessageBoxImage icnMessageBox = MessageBoxImage.Warning;
MessageBoxResult rsltMessageBox = System.Windows.MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);
switch (rsltMessageBox)
{
case MessageBoxResult.Yes:
return true;
case MessageBoxResult.No:
return false;
case MessageBoxResult.Cancel:
Environment.Exit(0);
break;
}
return false;
}
}
}