384 lines
9.6 KiB
C#
384 lines
9.6 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Eugen.ESystem.Windows.Forms
|
|
{
|
|
/// <summary>
|
|
/// An extended MessageBox with lot of customizing capabilities.
|
|
/// </summary>
|
|
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
|
|
/// <summary>
|
|
/// Contains the name of the program file
|
|
/// </summary>
|
|
public static string DllName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the program file
|
|
/// </summary>
|
|
public static string DllVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice
|
|
/// </summary>
|
|
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; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the caption of the message box
|
|
/// </summary>
|
|
public string Caption
|
|
{
|
|
set{_msgBox.Caption = value;}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the text of the message box
|
|
/// </summary>
|
|
public string Text
|
|
{
|
|
set{_msgBox.Message = value;}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the icon to show in the message box
|
|
/// </summary>
|
|
public Icon CustomIcon
|
|
{
|
|
set{_msgBox.CustomIcon = value;}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the icon to show in the message box
|
|
/// </summary>
|
|
public MessageBoxExIcon Icon
|
|
{
|
|
set{ _msgBox.StandardIcon = (MessageBoxIcon)Enum.Parse(typeof(MessageBoxIcon), value.ToString());}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the font for the text of the message box
|
|
/// </summary>
|
|
public Font Font
|
|
{
|
|
set{_msgBox.Font = value;}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets or Gets the ability of the user to save his/her response
|
|
/// </summary>
|
|
public bool AllowSaveResponse
|
|
{
|
|
get{ return _msgBox.AllowSaveResponse; }
|
|
set{ _msgBox.AllowSaveResponse = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the text to show to the user when saving his/her response
|
|
/// </summary>
|
|
public string SaveResponseText
|
|
{
|
|
set{_msgBox.SaveResponseText = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets or Gets wether the saved response if available should be used
|
|
/// </summary>
|
|
public bool UseSavedResponse
|
|
{
|
|
get{ return _useSavedResponse; }
|
|
set{ _useSavedResponse = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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
|
|
/// </summary>
|
|
public bool PlayAlsertSound
|
|
{
|
|
get{ return _msgBox.PlayAlertSound; }
|
|
set{ _msgBox.PlayAlertSound = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets or Gets the time in milliseconds for which the message box is displayed.
|
|
/// </summary>
|
|
public int Timeout
|
|
{
|
|
get{ return _msgBox.Timeout; }
|
|
set{ _msgBox.Timeout = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Controls the result that will be returned when the message box times out.
|
|
/// </summary>
|
|
public TimeoutResult TimeoutResult
|
|
{
|
|
get{ return _msgBox.TimeoutResult; }
|
|
set{ _msgBox.TimeoutResult = value; }
|
|
}
|
|
#endregion
|
|
|
|
#region Methods
|
|
/// <summary>
|
|
/// Shows the message box
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string Show()
|
|
{
|
|
return Show(null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the messsage box with the specified owner
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a custom button to the message box
|
|
/// </summary>
|
|
/// <param name="button">The button to add</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a custom button to the message box
|
|
/// </summary>
|
|
/// <param name="text">The text of the button</param>
|
|
/// <param name="val">The return value in case this button is clicked</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a standard button to the message box
|
|
/// </summary>
|
|
/// <param name="buttons">The standard button to add</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add standard buttons to the message box.
|
|
/// </summary>
|
|
/// <param name="buttons">The standard buttons to add</param>
|
|
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
|
|
/// <summary>
|
|
/// Ctor is internal because this can only be created by MBManager
|
|
/// </summary>
|
|
internal MessageBoxEx()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by the manager when it is disposed
|
|
/// </summary>
|
|
internal void Dispose()
|
|
{
|
|
if(_msgBox != null)
|
|
{
|
|
_msgBox.Dispose();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|