using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using AppRes = Eugen.ESystem.Properties.AppResources;
namespace Eugen.ESystem
{
public class DynamicMenuItems
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
if (startYear.Contains("-"))
{
startYear = startYear.Remove(startYear.IndexOf("-"));
}
else
{
startYear = startYear.Remove(startYear.IndexOf(" "));
}
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
string start = temp.Remove(temp.IndexOf(startYear) - 1);
string end = (temp.Remove(0, temp.IndexOf(" by")));
return String.Concat(start, " ", startYear, "-", currentYear, end);
}
}
#endregion
#region DLL-Info
///
/// Contains the name of the dll file.
///
public static string DllName { private set; get; }
///
/// Contains the version of the dll file.
///
public static string DllVersion { private set; get; }
///
/// Contains the copyright notice.
///
public static string Copyright { private set; get; }
///
/// Contains the name-, version- and copyright-information of the dll file.
///
public static string[] DllInfo { private set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
string[] dllInfo = new string[3];
dllInfo[0] = dllName;
dllInfo[1] = dllVersion;
dllInfo[2] = copyright;
DllInfo = dllInfo;
}
protected string ProgramName { private set; get; }
protected string ProgramVersion { private set; get; }
protected string ProgramCopyright { private set; get; }
#endregion
static DynamicMenuItems()
{
SetDllInfo();
}
public DynamicMenuItems()
{
SetDllInfo();
}
///
/// List of menu entries to be created
///
public string[] MenuItems { get; set; }
///
/// Menu into which the submenus are inserted
///
public ToolStripMenuItem Menu { get; set; }
///
/// Insert a menu item
///
/// Name of the Menu item
public void AddMenuItem(string menuItem)
{
ToolStripMenuItem menu = (ToolStripMenuItem)(Menu);
Menu.DropDownItems.Add(menuItem, null, new EventHandler(ToolStripMenuItem_Click));
}
///
/// Insert menu entry from a list
///
/// List of menu items
public void AddMenuItems(string[] menuList)
{
//Für jedes Element in der Liste einen Menüpunkt anlegen
foreach (var element in menuList)
{
AddMenuItem(element);
}
}
///
/// Check a menu item
///
/// Item name
public void CheckMenuItem(string selectedItem)
{
//Hier soll der Menüeintrag, der dem Übergebenen Wert entspricht, auf 'Checked = true' gesetzt werden
foreach (ToolStripMenuItem item in Menu.DropDownItems)
{
if (item.Text == selectedItem)
{
item.Checked = true;
}
else
{
item.Checked = false;
}
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem = (ToolStripMenuItem)sender;
CheckMenuItem(MenuItem.Text); //Menu auswählen
OnMyEvent(EventArgs.Empty); //Das eigene Event aufrufen
}
public event EventHandler MyEvent; //Ein eigens Event erstellen
//Eventhandler
protected virtual void OnMyEvent(EventArgs e)
{
EventHandler myEvent = MyEvent;
if (myEvent != null)
{
myEvent(this, e);
}
}
}
#region Exceptions
class UnableToSaveLicenseBundleException : ApplicationException
{
public UnableToSaveLicenseBundleException()
{ }
public UnableToSaveLicenseBundleException(string message) : base(message)
{ }
public UnableToSaveLicenseBundleException(string message, Exception inner) : base(message, inner)
{ }
}
#endregion
}