📄 form1.cs
字号:
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;
namespace SPAlarm
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private AlarmStyle style;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox1;
private AlarmTimeZone timezone;
private string currentpath;
private Alarm alarm;
private AlarmInfo pendingalarm;
private System.Windows.Forms.MenuItem menuItem4;
private DateTime pendingalarmtime;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private editalarmlist edalarm;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
style = new AlarmStyle();
currentpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
style.ReadStyle(currentpath+"\\"+loaddefaultstyle());
this.Text = style.formcaption;
SetTranslation(style.translation);
timezone = new AlarmTimeZone();
timezone.LoadXml(currentpath+"\\TimeZone.xml");
alarm = new Alarm();
alarm.LoadFromXml(currentpath+"\\Alarm.xml");
SwitchToNextAlarm();
edalarm = new editalarmlist();
}
private ArrayList getstylenames()
{
string[] files = Directory.GetFiles(currentpath,"style*.xml");
ArrayList stylenames = new ArrayList();
foreach(string f in files)
{
FileInfo fi = new FileInfo(f);
stylenames.Add(fi.Name);
}
return stylenames;
}
private void savedefaultstyle(string stylename)
{
StreamWriter w = File.CreateText(currentpath+"\\defaultstyle.txt");
w.WriteLine(stylename);
w.Close();
}
private string loaddefaultstyle()
{
if (!File.Exists(currentpath+"\\defaultstyle.txt"))
{
return "style.xml";
}
StreamReader r = File.OpenText(currentpath+"\\defaultstyle.txt");
string stylename = r.ReadLine();
r.Close();
return stylename;
}
private void SetTranslation(Hashtable translation)
{
SetTranslationMenu(menuItem1, "test", translation);
SetTranslationMenu(menuItem2, "menu", translation);
SetTranslationMenu(menuItem3, "exit", translation);
SetTranslationMenu(menuItem4, "edit", translation);
SetTranslationMenu(menuItem5, "timezone", translation);
SetTranslationMenu(menuItem6, "about", translation);
SetTranslationMenu(menuItem7, "disableall", translation);
SetTranslationMenu(menuItem8, "style", translation);
}
private void SetTranslationMenu(MenuItem menuitem, string index, Hashtable translation)
{
if (translation.ContainsKey(index))
{
menuitem.Text = (string)translation[index];
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.timer1 = new System.Windows.Forms.Timer();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.menuItem8 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);
//
// menuItem1
//
this.menuItem1.Text = "TestAlarm";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.MenuItems.Add(this.menuItem4);
this.menuItem2.MenuItems.Add(this.menuItem7);
this.menuItem2.MenuItems.Add(this.menuItem5);
this.menuItem2.MenuItems.Add(this.menuItem8);
this.menuItem2.MenuItems.Add(this.menuItem6);
this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.Text = "Menu";
//
// menuItem5
//
this.menuItem5.Text = "TimeZone";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem4
//
this.menuItem4.Text = "EditAlarm";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem7
//
this.menuItem7.Text = "Disable all Alarm";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem6
//
this.menuItem6.Text = "About";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem3
//
this.menuItem3.Text = "Exit";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// pictureBox1
//
this.pictureBox1.Size = new System.Drawing.Size(176, 180);
//
// menuItem8
//
this.menuItem8.Text = "Style";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
//
// Form1
//
this.Controls.Add(this.pictureBox1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//style.PaintStyle(sender, e);
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
DateTime disptime = timezone.LocalToCurrent(DateTime.Now);
pictureBox1.Image = style.DisplayImage(disptime);
if ((pendingalarm != null) && (disptime >= pendingalarmtime))
{
timer1.Enabled = false;
pendingalarm.DoAction(disptime);
SwitchToNextAlarm();
timer1.Enabled = true;
}
}
private void SwitchToNextAlarm()
{
DateTime now = timezone.LocalToCurrent(DateTime.Now);
pendingalarm = alarm.GetPendingAlarm(now);
string runappname = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase+'\0';
if (pendingalarm == null)
{
CeRunAppAtTime(runappname.ToCharArray(), null);
}
else
{
pendingalarmtime = pendingalarm.GetNextAlarmTime(now);
DateTime runapptime = timezone.CurrentToLocal(pendingalarmtime) - new TimeSpan(0,0,30);
SystemTime cetime = new SystemTime(runapptime);
CeRunAppAtTime(runappname.ToCharArray(), cetime.ToByteArray());
}
style.UpdateAlarmInfo(pendingalarm, now);
}
[DllImport("coredll", EntryPoint="CeRunAppAtTime", SetLastError=true)]
private extern static bool CeRunAppAtTime(char[] applname, byte[] lpSystemTime);
private void menuItem4_Click(object sender, System.EventArgs e)
{
edalarm.LoadFromXml(currentpath+"\\alarm.xml");
if (edalarm.ShowDialog() == DialogResult.OK)
{
edalarm.SaveToXml(currentpath+"\\alarm.xml");
alarm.LoadFromXml(currentpath+"\\alarm.xml");
SwitchToNextAlarm();
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
if (pendingalarm == null) { return; }
DateTime disptime = timezone.LocalToCurrent(DateTime.Now);
pendingalarm.DoAction(disptime);
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
FmTimeZone fm = new FmTimeZone();
fm.EditTimeZone(timezone);
SwitchToNextAlarm();
fm.Dispose();
timezone.SaveXml(currentpath+"\\TimeZone.xml");
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
fmAbout about = new fmAbout();
about.Text = menuItem6.Text;
SetTranslationMenu(about.menuItem1, "done", style.translation);
about.ShowDialog();
about.Dispose();
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
alarm.DisableAllAlarms();
alarm.SaveToXml(currentpath+"\\alarm.xml");
SwitchToNextAlarm();
}
private void menuItem8_Click(object sender, System.EventArgs e)
{
FmStyle fmstyle = new FmStyle();
fmstyle.Text = menuItem8.Text;
SetTranslationMenu(fmstyle.menuItem1, "done", style.translation);
string newstylefile = fmstyle.ChooseStyles(getstylenames(), loaddefaultstyle());
savedefaultstyle(newstylefile);
style.ReadStyle(currentpath+"\\"+newstylefile);
fmstyle.Dispose();
SwitchToNextAlarm();
}
}
/// <summary>
/// This structure represents a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.
/// </summary>
/// <remarks>Wraps the native <b>SYSTEMTIME</b> structure.</remarks>
public class SystemTime
{
protected byte[] flatStruct = new byte[ 16 ];
#region Flat structure offset constants
/// <summary>
/// Offset within structure to Year value.
/// </summary>
protected const short wYearOffset = 0;
/// <summary>
/// Offset within structure to Month value.
/// </summary>
protected const short wMonthOffset = 2;
/// <summary>
/// Offset within structure to DayOfWeek value.
/// </summary>
protected const short wDayOfWeekOffset = 4;
/// <summary>
/// Offset within structure to Day value.
/// </summary>
protected const short wDayOffset = 6;
/// <summary>
/// Offset within structure to Hour value.
/// </summary>
protected const short wHourOffset = 8;
/// <summary>
/// Offset within structure to Minute value.
/// </summary>
protected const short wMinuteOffset = 10;
/// <summary>
/// Offset within structure to Second value.
/// </summary>
protected const short wSecondOffset = 12;
/// <summary>
/// Offset within structure to Millisecond value.
/// </summary>
protected const short wMillisecondsOffset = 14;
#endregion
/// <summary>
/// Represents an empty SystemTime structure.
/// </summary>
public static readonly SystemTime Empty = new SystemTime(0, 0, 0, 0, 0, 0);
#region ctor(byte[])
/// <summary>
/// Construct a SystemTime from a byte array
/// </summary>
/// <remarks>This is used when setting a time zone,
/// which contains two embedded SystemTime structures.</remarks>
/// <param name="bytes">Byte Array containing SystemTime data.</param>
public SystemTime( byte[] bytes ) : this( bytes, 0 )
{
}
#endregion
#region ctor(byte[], int)
/// <summary>
/// Construct a SystemTime from a portion of a byte array.
/// </summary>
/// <remarks>This is used when setting a time zone,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -