using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Runtime.InteropServices;
namespace SleepTimer
{
public partial class SleepTimer : Form
{
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
int sec = 0;
int min = 0;
int ore = 0;
bool stato = false;
public SleepTimer()
{
InitializeComponent();
System.Timers.
Timer aTimer
= new System.Timers.
Timer();
aTimer.
Elapsed += new ElapsedEventHandler
(EventoProgrammato
);
aTimer.Interval = 1000;
aTimer.Enabled = true;
GC.KeepAlive(aTimer);
}
private void sec_bar_ValueChanged(object sender, EventArgs e)
{
sec = this.sec_bar.Value;
this.sec_text.Text = Convert.ToString(sec) + " Sec";
timer();
}
private void min_bar_ValueChanged(object sender, EventArgs e)
{
min = this.min_bar.Value;
this.min_text.Text = Convert.ToString(min) + " Min";
timer();
}
private void ore_bar_ValueChanged(object sender, EventArgs e)
{
ore = this.ore_bar.Value;
this.ore_text.Text = Convert.ToString(ore) + " Ore";
timer();
}
private void timer()
{
string sec_t;
string min_t;
string ore_t;
if (sec < 10)
{
sec_t = "0" + sec;
}
else
{
sec_t = Convert.ToString(sec);
}
if (min < 10)
{
min_t = "0" + min;
}
else
{
min_t = Convert.ToString(min);
}
if (ore < 10)
{
ore_t = "0" + ore;
}
else
{
ore_t = Convert.ToString(ore);
}
this.timer_text.Text = ore_t + ":" + min_t + ":" + sec_t;
this.timer_menu.Text = ore_t + ":" + min_t + ":" + sec_t;
}
private void start_but_Click(object sender, EventArgs e)
{
stato = true;
this.start_but.Enabled = false;
this.stop_but.Enabled = true;
this.start_menu.Enabled = false;
this.stop_menu.Enabled = true;
this.sec_bar.Enabled = false;
this.min_bar.Enabled = false;
this.ore_bar.Enabled = false;
this.shut.Enabled = false;
this.restart.Enabled = false;
this.standby.Enabled = false;
this.sosp.Enabled = false;
}
private void stop_but_Click(object sender, EventArgs e)
{
stop();
}
private void stop()
{
stato = false;
this.start_but.Enabled = true;
this.stop_but.Enabled = false;
this.start_menu.Enabled = true;
this.stop_menu.Enabled = false;
this.sec_bar.Enabled = true;
this.min_bar.Enabled = true;
this.ore_bar.Enabled = true;
this.shut.Enabled = true;
this.restart.Enabled = true;
this.standby.Enabled = true;
this.sosp.Enabled = true;
this.start_menu.Enabled = true;
this.stop_menu.Enabled = false;
}
private void EventoProgrammato(object source, ElapsedEventArgs e)
{
if (stato == true)
{
string[] timer
= this.
timer_text.
Text.
Split(new string[] { ":" }, StringSplitOptions.
None);
var sec_timer = Convert.ToInt32(timer[2]);
var min_timer = Convert.ToInt32(timer[1]);
var ore_timer = Convert.ToInt32(timer[0]);
if (sec_timer == 0 && min_timer == 0 && ore_timer == 0)
{
stop();
this.icon.ShowBalloonTip(3000, "Sleep Timer", "Fine!!!", ToolTipIcon.Info);
{
ExitWindowsEx(1, 0);
}
else if (this.
restart.
Checked == true)
{
ExitWindowsEx(2, 0);
}
else if (this.
standby.
Checked == true)
{
Application.SetSuspendState(PowerState.Suspend, true, true);
}
else if (this.
sosp.
Checked == true)
{
Application.SetSuspendState(PowerState.Hibernate, true, true);
}
}
else
{
if (sec_timer == 0 && min_timer == 30 && ore_timer == 0)
{
this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 30 minuti...", ToolTipIcon.Info);
}
else if (sec_timer == 0 && min_timer == 10 && ore_timer == 0)
{
this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 10 minuti...", ToolTipIcon.Info);
}
else if (sec_timer == 0 && min_timer == 5 && ore_timer == 0)
{
this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 5 minuti...", ToolTipIcon.Info);
}
else if (sec_timer == 0 && min_timer == 1 && ore_timer == 0)
{
this.icon.ShowBalloonTip(3000, "Sleep Timer", "Manca solo un minuto...", ToolTipIcon.Info);
}
if(sec_timer == 0 && min_timer == 0)
{
ore_timer = ore_timer - 1;
min_timer = 59;
sec_timer = 59;
}
else if (sec_timer == 0)
{
min_timer = min_timer - 1;
sec_timer = 59;
}
else
{
sec_timer = sec_timer - 1;
}
string sec_t;
string min_t;
string ore_t;
if (sec_timer < 10)
{
sec_t = "0" + sec_timer;
}
else
{
sec_t = Convert.ToString(sec_timer);
}
if (min_timer < 10)
{
min_t = "0" + min_timer;
}
else
{
min_t = Convert.ToString(min_timer);
}
if (ore_timer < 10)
{
ore_t = "0" + ore_timer;
}
else
{
ore_t = Convert.ToString(ore_timer);
}
this.sec_bar.Value = sec_timer;
this.min_bar.Value = min_timer;
this.ore_bar.Value = ore_timer;
this.timer_text.Text = ore_t + ":" + min_t + ":" + sec_t;
this.timer_menu.Text = ore_t + ":" + min_t + ":" + sec_t;
}
}
}
private void SleepTimer_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Visible = false;
}
}
private void open_menu_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void exit_menu_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void icon_DoubleClick(object sender, EventArgs e)
{
this.Visible = true;
}
}
}