using System; using System.Drawing; using System.Windows.Forms; using TBF.UiBridge; namespace TBF.UI.Shared { public partial class ModelessActivityForm : Form { /// /// Called from the state machine when an operation forces a modeless dialog close. /// public void CloseForm(object sender, EventArgs args) { if (CloseFormHandler == null) return; try { CloseFormHandler(sender, null); } catch (Exception) { } } public event EventHandler 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; /// /// Constructor to be used by the application /// /// Window title /// Displayed message public ModelessActivityForm() { InitializeComponent(); this.Icon = Properties.Resources.TBF_icon; ControlBox = false; CloseFormHandler += HandleCloseFormCommand; } private void ModelessActivityForm_FormClosing(object sender, FormClosingEventArgs e) { CloseFormHandler -= HandleCloseFormCommand; } void HandleCloseFormCommand(object sender, EventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(OnCloseForm), sender, args); } else { OnCloseForm(sender, args); } } void OnCloseForm(object sender, EventArgs args) { DialogResult = DialogResult.Cancel; Close(); } 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(OnActivity), sndr, args); } else OnActivity(sndr, args); }; } } void OnActivity(object sender, ActivityEventArgs args) { activityStatusLabel.Text = args.Activity; } } }