using System; using System.Drawing; using System.Windows.Forms; namespace TracingDB.Forms { public partial class ModelessForm : Form { /// /// Called from the state machine when an operation forces a modeless dialog close. /// public static void UpdateMessage(MessageEventArgs args) { if (UpdateMessageHandler == null) return; try { UpdateMessageHandler(null, args); } catch (Exception) { } } public static event EventHandler UpdateMessageHandler; void OnUpdateMessage(object sender, MessageEventArgs args) { Message = args.Message; if (Message != null) label1.Text = 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) { } } public static event EventHandler 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; /// /// Constructor to be used by the application /// /// Window title /// Displayed message public ModelessForm() { InitializeComponent(); ControlBox = false; 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); }; } 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 } } }