using System; using System.Drawing; using System.IO; using System.Reflection; using System.Windows.Forms; namespace Eugen.ESystem.Windows.Forms { /// /// An extended MessageBox with lot of customizing capabilities. /// public class MessageBoxEx { #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(); string icon = Application.StartupPath + "\\Info.bmp"; //Assembly Datum und Zeit static DateTime value = AssemblyDateTime(); string date = value.ToShortDateString(); 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); } 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 { return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear)); } } #endregion #region DLL-Info /// /// Contains the name of the program file /// public static string DllName { set; get; } /// /// Contains the version of the program file /// public static string DllVersion { set; get; } /// /// Contains the copyright notice /// public static string Copyright { set; get; } private static void SetDllInfo() { //Name, Version und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = copyright; } #endregion #region Fields private MessageBoxExForm _msgBox = new MessageBoxExForm(); private bool _useSavedResponse = true; private string _name = null; #endregion #region Properties internal string Name { get{ return _name; } set{ _name = value; } } /// /// Sets the caption of the message box /// public string Caption { set{_msgBox.Caption = value;} } /// /// Sets the text of the message box /// public string Text { set{_msgBox.Message = value;} } /// /// Sets the icon to show in the message box /// public Icon CustomIcon { set{_msgBox.CustomIcon = value;} } /// /// Sets the icon to show in the message box /// public MessageBoxExIcon Icon { set{ _msgBox.StandardIcon = (MessageBoxIcon)Enum.Parse(typeof(MessageBoxIcon), value.ToString());} } /// /// Sets the font for the text of the message box /// public Font Font { set{_msgBox.Font = value;} } /// /// Sets or Gets the ability of the user to save his/her response /// public bool AllowSaveResponse { get{ return _msgBox.AllowSaveResponse; } set{ _msgBox.AllowSaveResponse = value; } } /// /// Sets the text to show to the user when saving his/her response /// public string SaveResponseText { set{_msgBox.SaveResponseText = value; } } /// /// Sets or Gets wether the saved response if available should be used /// public bool UseSavedResponse { get{ return _useSavedResponse; } set{ _useSavedResponse = value; } } /// /// Sets or Gets wether an alert sound is played while showing the message box. /// The sound played depends on the the Icon selected for the message box /// public bool PlayAlsertSound { get{ return _msgBox.PlayAlertSound; } set{ _msgBox.PlayAlertSound = value; } } /// /// Sets or Gets the time in milliseconds for which the message box is displayed. /// public int Timeout { get{ return _msgBox.Timeout; } set{ _msgBox.Timeout = value; } } /// /// Controls the result that will be returned when the message box times out. /// public TimeoutResult TimeoutResult { get{ return _msgBox.TimeoutResult; } set{ _msgBox.TimeoutResult = value; } } #endregion #region Methods /// /// Shows the message box /// /// public string Show() { return Show(null); } /// /// Shows the messsage box with the specified owner /// /// /// public string Show(IWin32Window owner) { if(_useSavedResponse && this.Name != null) { string savedResponse = MessageBoxExManager.GetSavedResponse(this); if( savedResponse != null) return savedResponse; } if(owner == null) { _msgBox.ShowDialog(); } else { _msgBox.ShowDialog(owner); } if(this.Name != null) { if(_msgBox.AllowSaveResponse && _msgBox.SaveResponse) MessageBoxExManager.SetSavedResponse(this, _msgBox.Result); else MessageBoxExManager.ResetSavedResponse(this.Name); } else { Dispose(); } return _msgBox.Result; } /// /// Add a custom button to the message box /// /// The button to add public void AddButton(MessageBoxExButton button) { if(button == null) throw new ArgumentNullException("button","A null button cannot be added"); _msgBox.Buttons.Add(button); if(button.IsCancelButton) { _msgBox.CustomCancelButton = button; } } /// /// Add a custom button to the message box /// /// The text of the button /// The return value in case this button is clicked public void AddButton(string text, string val) { if(text == null) throw new ArgumentNullException("text","Text of a button cannot be null"); if(val == null) throw new ArgumentNullException("val","Value of a button cannot be null"); MessageBoxExButton button = new MessageBoxExButton(); button.Text = text; button.Value = val; AddButton(button); } /// /// Add a standard button to the message box /// /// The standard button to add public void AddButton(MessageBoxExButtons button) { string buttonText = MessageBoxExManager.GetLocalizedString(button.ToString()); if(buttonText == null) { buttonText = button.ToString(); } string buttonVal = button.ToString(); MessageBoxExButton btn = new MessageBoxExButton(); btn.Text = buttonText; btn.Value = buttonVal; if(button == MessageBoxExButtons.Cancel) { btn.IsCancelButton = true; } AddButton(btn); } /// /// Add standard buttons to the message box. /// /// The standard buttons to add public void AddButtons(MessageBoxButtons buttons) { switch(buttons) { case MessageBoxButtons.OK: AddButton(MessageBoxExButtons.Ok); break; case MessageBoxButtons.AbortRetryIgnore: AddButton(MessageBoxExButtons.Abort); AddButton(MessageBoxExButtons.Retry); AddButton(MessageBoxExButtons.Ignore); break; case MessageBoxButtons.OKCancel: AddButton(MessageBoxExButtons.Ok); AddButton(MessageBoxExButtons.Cancel); break; case MessageBoxButtons.RetryCancel: AddButton(MessageBoxExButtons.Retry); AddButton(MessageBoxExButtons.Cancel); break; case MessageBoxButtons.YesNo: AddButton(MessageBoxExButtons.Yes); AddButton(MessageBoxExButtons.No); break; case MessageBoxButtons.YesNoCancel: AddButton(MessageBoxExButtons.Yes); AddButton(MessageBoxExButtons.No); AddButton(MessageBoxExButtons.Cancel); break; } } #endregion #region Ctor /// /// Ctor is internal because this can only be created by MBManager /// internal MessageBoxEx() { SetDllInfo(); } /// /// Called by the manager when it is disposed /// internal void Dispose() { if(_msgBox != null) { _msgBox.Dispose(); } } #endregion } }