tbf/TracingDB/Forms/ModelessForm.cs

93 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Drawing;
using System.Windows.Forms;
namespace TracingDB.Forms
{
public partial class ModelessForm : Form
{
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public static void UpdateMessage(MessageEventArgs args)
{
if (UpdateMessageHandler == null) return;
try { UpdateMessageHandler(null, args); }
catch (Exception) { }
}
public static event EventHandler<MessageEventArgs> UpdateMessageHandler;
void OnUpdateMessage(object sender, MessageEventArgs args)
{
Message = args.Message;
if (Message != null) label1.Text = Message;
}
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public static void CloseForm()
{
if (CloseFormHandler == null) return;
try { CloseFormHandler(null, null); }
catch (Exception) { }
}
public static event EventHandler<EventArgs> CloseFormHandler;
void OnCloseForm(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
public string Title;
public string Message;
public Color BackgroundColor;
public Color TextColor;
public Font TextFont;
public string FontFamily;
public int FontSize;
public FontStyle FontStyle;
/// <summary>
/// Constructor to be used by the application
/// </summary>
/// <param name="title">Window title</param>
/// <param name="message">Displayed message</param>
public ModelessForm()
{
InitializeComponent();
ControlBox = false;
UpdateMessageHandler += delegate(object sender, MessageEventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler<MessageEventArgs>(OnUpdateMessage), sender, args); }
else OnUpdateMessage(sender, args);
};
CloseFormHandler += delegate(object sender, EventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler<EventArgs>(OnCloseForm), sender, args); }
else OnCloseForm(sender, args);
};
}
private void ModelessForm_Load(object sender, EventArgs e)
{
if (Title != null) Text = Title;
if (Message != null) label1.Text = Message;
if (BackgroundColor != null) BackColor = BackgroundColor;
if (TextColor != null) ForeColor = TextColor;
if (FontFamily != null) label1.Font = new Font(FontFamily, FontSize, FontStyle);
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
}
}
}