tbf/TBF/Forms/ModelessActivityForm.cs

90 lines
3.0 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using TBF.UiBridge;
namespace TBF.Forms
{
public partial class ModelessActivityForm : Form
{
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public void CloseForm(object sender, EventArgs args)
{
if (CloseFormHandler == null) return;
try { CloseFormHandler(sender, null); }
catch (Exception) { }
}
public event EventHandler<EventArgs> CloseFormHandler;
public string Title;
public string Message;
public Color BackgroundColor;
public Color TextColor;
public Font TextFont;
public string FontFamily;
public int FontSize;
public FontStyle FontStyle;
public bool StartActivityHandler;
/// <summary>
/// Constructor to be used by the application
/// </summary>
/// <param name="title">Window title</param>
/// <param name="message">Displayed message</param>
public ModelessActivityForm()
{
InitializeComponent();
ControlBox = false;
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 (!string.IsNullOrEmpty(Title)) Text = Title;
if (!string.IsNullOrEmpty(Message)) label1.Text = Message;
if (BackgroundColor != null) BackColor = BackgroundColor;
if (TextColor != null) ForeColor = TextColor;
if (!string.IsNullOrEmpty(FontFamily))
{
label1.Font = new Font(FontFamily, FontSize, FontStyle);
activityStatusLabel.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
if (StartActivityHandler)
{
Height += (int)textHeight; /// Form height calculated from the text width
Bridge.ActivityHandler += delegate(object sndr, ActivityEventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler<ActivityEventArgs>(OnActivity), sndr, args); }
else OnActivity(sndr, args);
};
}
}
void OnActivity(object sender, ActivityEventArgs args)
{
activityStatusLabel.Text = args.Activity;
}
void OnCloseForm(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}