-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
208 lines (180 loc) · 6.67 KB
/
Form1.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
206
207
208
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Avisador
{
public partial class frmAvisador : Form
{
const string DEFAULT = "default.wav";
const string FILEULTIMTO = "ultimTo.txt";
Timer tiempoRestante;
int segonsRestants;
private string musica = "";
private System.Windows.Forms.NotifyIcon notify;
public frmAvisador()
{
byte[] data;
InitializeComponent();
if (File.Exists(FILEULTIMTO))
{
musica = File.ReadAllText(FILEULTIMTO);
if (!File.Exists(musica))
musica = "";
}
if (!File.Exists(musica))
{
musica = DEFAULT;
data = new byte[Resource1.Gong.Length];
Resource1.Gong.CopyTo(new MemoryStream(data));
File.WriteAllBytes(musica, data);
}
lblNombreTono.Text = Path.GetFileNameWithoutExtension(musica);
this.BringToFront();
if (!File.Exists(musica)) this.Close();
else
{
tiempoRestante = new Timer();
tiempoRestante.Tick += new EventHandler(CambiaTiempoRestante);
tiempoRestante.Interval = 1000;
btnSig.Visible = Debugger.IsAttached;
//notify
this.notify = new NotifyIcon();
this.notify.Icon = icono.tempIco;
this.notify.Text = "temps";
this.notify.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
Resize += new EventHandler(ResizeEvent);
listaTemporizadores1.TemporizadorSiguiente += new TemporizadorEventHandler(TempSegNotifyEvent);
}
}
private void TempSegNotifyEvent(temporizador temp)
{
notify.Tag = temp;
}
private void ResizeEvent(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
this.notify.Visible = true;
this.notify.Tag = listaTemporizadores1.Siguiente;
notify.BalloonTipTitle = "La aplicació continua funcionant!!";
notify.BalloonTipText = "Quan vulguis tornarla a posar fes-li clic ;)";
notify.ShowBalloonTip(3000);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.notify.Visible = false;
Show();
this.notify.Tag = null;
WindowState = FormWindowState.Normal;
}
public bool Continuar
{
get { return musica != ""; }
}
private void CambiaTiempoRestante(object sender, EventArgs e)
{
segonsRestants--;
if (segonsRestants >= 0)
{
int hores = segonsRestants / 3600;
int minutos = (segonsRestants - (hores * 3600)) / 60;
int segundos = segonsRestants - hores * 3600 - minutos * 60;
lblTiempoRestante.Text = hores + ":" + minutos + ":" + segundos;
this.notify.Text = "Queda: " + lblTiempoRestante.Text;
}
if (segonsRestants <= 0)
{
tiempoRestante.Enabled = false;
SuenaAlarma();
if (notify.Tag == null)
listaTemporizadores1.HoraSiguiente();
else
listaTemporizadores1.PonerTemporizador(notify.Tag as temporizador);
}
}
private void SuenaAlarma()
{
playSound(musica);
}
private void playSound(string path)
{
try
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = path;
player.Load();
player.Play();
}
catch { MessageBox.Show("El archivo no se ha podio abrir"); }
}
private void listaTemporizadores1_Hora(string hora, int segundosRestantes)
{
lblTiempoParaSonar.Text = hora;
this.segonsRestants = segundosRestantes;
if (this.segonsRestants != 0)
tiempoRestante.Enabled = true;
}
private void btnAñadirTemp_Click(object sender, EventArgs e)
{
temporizador temp = new temporizador(numericUpDown1, numericUpDown2);
listaTemporizadores1.Añadir(temp);
if (!tiempoRestante.Enabled)
{
segonsRestants = 0;
tiempoRestante.Enabled = true;
label1.Visible = true;
label2.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
segonsRestants = 4;
}
private void frmAvisador_FormClosing(object sender, FormClosingEventArgs e)
{
if (Equals(musica, DEFAULT))
File.Delete(musica);
musica = "";
}
public void Reset()
{
lblTiempoParaSonar.Text = "";
lblTiempoRestante.Text = "";
segonsRestants = 0;
listaTemporizadores1.Reset();
tiempoRestante.Enabled = false;
label1.Visible = false;
label2.Visible = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
FileStream fileRecord;
StreamWriter fileStream;
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Audio Files (.wav)|*.wav";
if (dialog.ShowDialog() == DialogResult.OK)
{
musica = dialog.FileName;
lblNombreTono.Text = Path.GetFileNameWithoutExtension(musica);
if (!string.IsNullOrEmpty(musica))
{
fileRecord = new FileStream(FILEULTIMTO, FileMode.OpenOrCreate);
fileStream = new StreamWriter(fileRecord, Encoding.UTF8);
fileStream.WriteLine(musica);
fileStream.Close();
fileRecord.Close();
}
}
}
}
}