Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
SleepTimer - SleepTimer.cs

SleepTimer.cs

Caricato da: Roby94
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers;
  10. using System.Runtime.InteropServices;
  11.  
  12. namespace SleepTimer
  13. {
  14.     public partial class SleepTimer : Form
  15.     {
  16.         [DllImport("user32.dll")]
  17.         public static extern int ExitWindowsEx(int uFlags, int dwReason);
  18.  
  19.         int sec = 0;
  20.         int min = 0;
  21.         int ore = 0;
  22.         bool stato = false;
  23.  
  24.         public SleepTimer()
  25.         {
  26.             InitializeComponent();
  27.             System.Timers.Timer aTimer = new System.Timers.Timer();
  28.             aTimer.Elapsed += new ElapsedEventHandler(EventoProgrammato);
  29.             aTimer.Interval = 1000;
  30.             aTimer.Enabled = true;
  31.             GC.KeepAlive(aTimer);
  32.         }
  33.  
  34.         private void sec_bar_ValueChanged(object sender, EventArgs e)
  35.         {
  36.             sec = this.sec_bar.Value;
  37.             this.sec_text.Text = Convert.ToString(sec) + " Sec";
  38.             timer();
  39.         }
  40.  
  41.         private void min_bar_ValueChanged(object sender, EventArgs e)
  42.         {
  43.             min = this.min_bar.Value;
  44.             this.min_text.Text = Convert.ToString(min) + " Min";
  45.             timer();
  46.         }
  47.  
  48.         private void ore_bar_ValueChanged(object sender, EventArgs e)
  49.         {
  50.             ore = this.ore_bar.Value;
  51.             this.ore_text.Text = Convert.ToString(ore) + " Ore";
  52.             timer();
  53.         }
  54.  
  55.         private void timer()
  56.         {
  57.             string sec_t;
  58.             string min_t;
  59.             string ore_t;
  60.             if (sec < 10)
  61.             {
  62.                 sec_t = "0" + sec;
  63.             }
  64.             else
  65.             {
  66.                 sec_t = Convert.ToString(sec);
  67.             }
  68.             if (min < 10)
  69.             {
  70.                 min_t = "0" + min;
  71.             }
  72.             else
  73.             {
  74.                 min_t = Convert.ToString(min);
  75.             }
  76.             if (ore < 10)
  77.             {
  78.                 ore_t = "0" + ore;
  79.             }
  80.             else
  81.             {
  82.                 ore_t = Convert.ToString(ore);
  83.             }
  84.             this.timer_text.Text = ore_t + ":" + min_t + ":" + sec_t;
  85.             this.timer_menu.Text = ore_t + ":" + min_t + ":" + sec_t;
  86.         }
  87.        
  88.         private void start_but_Click(object sender, EventArgs e)
  89.         {
  90.             stato = true;
  91.             this.start_but.Enabled = false;
  92.             this.stop_but.Enabled = true;
  93.             this.start_menu.Enabled = false;
  94.             this.stop_menu.Enabled = true;
  95.             this.sec_bar.Enabled = false;
  96.             this.min_bar.Enabled = false;
  97.             this.ore_bar.Enabled = false;
  98.             this.shut.Enabled = false;
  99.             this.restart.Enabled = false;
  100.             this.standby.Enabled = false;
  101.             this.sosp.Enabled = false;
  102.         }
  103.  
  104.         private void stop_but_Click(object sender, EventArgs e)
  105.         {
  106.             stop();
  107.         }
  108.  
  109.         private void stop()
  110.         {
  111.             stato = false;
  112.             this.start_but.Enabled = true;
  113.             this.stop_but.Enabled = false;
  114.             this.start_menu.Enabled = true;
  115.             this.stop_menu.Enabled = false;
  116.             this.sec_bar.Enabled = true;
  117.             this.min_bar.Enabled = true;
  118.             this.ore_bar.Enabled = true;
  119.             this.shut.Enabled = true;
  120.             this.restart.Enabled = true;
  121.             this.standby.Enabled = true;
  122.             this.sosp.Enabled = true;
  123.             this.start_menu.Enabled = true;
  124.             this.stop_menu.Enabled = false;
  125.         }
  126.  
  127.         private void EventoProgrammato(object source, ElapsedEventArgs e)
  128.         {
  129.             if (stato == true)
  130.             {
  131.                 string[] timer = this.timer_text.Text.Split(new string[] { ":" }, StringSplitOptions.None);
  132.                 var sec_timer = Convert.ToInt32(timer[2]);
  133.                 var min_timer = Convert.ToInt32(timer[1]);
  134.                 var ore_timer = Convert.ToInt32(timer[0]);
  135.                 if (sec_timer == 0 && min_timer == 0 && ore_timer == 0)
  136.                 {
  137.                     stop();
  138.                     this.icon.ShowBalloonTip(3000, "Sleep Timer", "Fine!!!", ToolTipIcon.Info);
  139.                     if(this.shut.Checked == true)
  140.                     {
  141.                         ExitWindowsEx(1, 0);
  142.                     }
  143.                     else if (this.restart.Checked == true)
  144.                     {
  145.                         ExitWindowsEx(2, 0);
  146.                     }
  147.                     else if (this.standby.Checked == true)
  148.                     {
  149.                         Application.SetSuspendState(PowerState.Suspend, true, true);
  150.                     }
  151.                     else if (this.sosp.Checked == true)
  152.                     {
  153.                         Application.SetSuspendState(PowerState.Hibernate, true, true);
  154.                     }
  155.                 }
  156.                 else
  157.                 {
  158.                     if (sec_timer == 0 && min_timer == 30 && ore_timer == 0)
  159.                     {
  160.                         this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 30 minuti...", ToolTipIcon.Info);
  161.                     }
  162.                     else if (sec_timer == 0 && min_timer == 10 && ore_timer == 0)
  163.                     {
  164.                         this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 10 minuti...", ToolTipIcon.Info);
  165.                     }
  166.                     else if (sec_timer == 0 && min_timer == 5 && ore_timer == 0)
  167.                     {
  168.                         this.icon.ShowBalloonTip(3000, "Sleep Timer", "Mancono solo 5 minuti...", ToolTipIcon.Info);
  169.                     }
  170.                     else if (sec_timer == 0 && min_timer == 1 && ore_timer == 0)
  171.                     {
  172.                         this.icon.ShowBalloonTip(3000, "Sleep Timer", "Manca solo un minuto...", ToolTipIcon.Info);
  173.                     }
  174.                     if(sec_timer == 0 && min_timer == 0)
  175.                     {
  176.                         ore_timer = ore_timer - 1;
  177.                         min_timer = 59;
  178.                         sec_timer = 59;
  179.                     }
  180.                     else if (sec_timer == 0)
  181.                     {
  182.                         min_timer = min_timer - 1;
  183.                         sec_timer = 59;
  184.                     }
  185.                     else
  186.                     {
  187.                         sec_timer = sec_timer - 1;
  188.                     }
  189.                     string sec_t;
  190.                     string min_t;
  191.                     string ore_t;
  192.                     if (sec_timer < 10)
  193.                     {
  194.                         sec_t = "0" + sec_timer;
  195.                     }
  196.                     else
  197.                     {
  198.                         sec_t = Convert.ToString(sec_timer);
  199.                     }
  200.                     if (min_timer < 10)
  201.                     {
  202.                         min_t = "0" + min_timer;
  203.                     }
  204.                     else
  205.                     {
  206.                         min_t = Convert.ToString(min_timer);
  207.                     }
  208.                     if (ore_timer < 10)
  209.                     {
  210.                         ore_t = "0" + ore_timer;
  211.                     }
  212.                     else
  213.                     {
  214.                         ore_t = Convert.ToString(ore_timer);
  215.                     }
  216.                     this.sec_bar.Value = sec_timer;
  217.                     this.min_bar.Value = min_timer;
  218.                     this.ore_bar.Value = ore_timer;
  219.                     this.timer_text.Text = ore_t + ":" + min_t + ":" + sec_t;
  220.                     this.timer_menu.Text = ore_t + ":" + min_t + ":" + sec_t;
  221.                 }
  222.             }
  223.         }
  224.  
  225.         private void SleepTimer_FormClosing(object sender, FormClosingEventArgs e)
  226.         {
  227.             if (e.CloseReason == CloseReason.UserClosing)
  228.             {
  229.                 e.Cancel = true;
  230.                 this.Visible = false;
  231.             }
  232.         }
  233.  
  234.         private void open_menu_Click(object sender, EventArgs e)
  235.         {
  236.             this.Visible = true;
  237.         }
  238.  
  239.         private void exit_menu_Click(object sender, EventArgs e)
  240.         {
  241.             Application.Exit();
  242.         }
  243.  
  244.         private void icon_DoubleClick(object sender, EventArgs e)
  245.         {
  246.             this.Visible = true;
  247.         }
  248.     }
  249. }