using System;
using System.Drawing;
using System.Windows.Forms;
namespace Common.Forms
{
public partial class ModelessForm : Form
{
///
/// Constructor to be used by the application
///
/// Window title
/// Displayed message
public ModelessForm(string message, int backArgb = 0, int textArgb = 0, Font textFont = null, string title = null)
{
InitializeComponent();
ControlBox = false;
label1.Text = (message != null) ? message : "Please wait";
BackColor = (backArgb != 0) ? Color.FromArgb(backArgb) : Color.LightGreen;
ForeColor = (textArgb != 0) ? Color.FromArgb(textArgb) : Color.Black;
label1.Font = (textFont != null) ? textFont : new Font("Arial", 14, FontStyle.Regular);
Text = (title != null) ? title : string.Empty;
float textWidth = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Width;
float textHeight = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Height;
Width = (int)textWidth + 120; /// Form width calculated from the text width
Height += (int)textHeight; /// Form height calculated from the text height
UpdateMessageHandler += delegate(object sender, MessageEventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler(OnUpdateMessage), sender, args); }
else OnUpdateMessage(sender, args);
};
CloseFormHandler += delegate(object sender, EventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler(OnCloseForm), sender, args); }
else OnCloseForm(sender, args);
};
}
///
/// Called from the state machine when an operation forces a modeless dialog close.
///
public void UpdateMessage(MessageEventArgs args)
{
if (UpdateMessageHandler == null) return;
try { UpdateMessageHandler(null, args); }
catch (Exception) { }
}
public event EventHandler UpdateMessageHandler;
void OnUpdateMessage(object sender, MessageEventArgs args)
{
if (args.Message != null) label1.Text = args.Message;
}
///
/// Called from the state machine when an operation forces a modeless dialog close.
///
public static void CloseForm()
{
if (CloseFormHandler == null) return;
try { CloseFormHandler(null, null); }
catch (Exception) { }
}
static public event EventHandler CloseFormHandler;
void OnCloseForm(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}