/// <summary> /// MessageShowType型を綺麗な日本語文字列に変換 /// </summary> /// <param name="type"></param> /// <returns></returns> public static string Pretty(this MessageShowType type) { switch (type) { case MessageShowType.Information: case MessageShowType.InformationOkCancel: return("通知"); case MessageShowType.Warning: case MessageShowType.WarningOkCancel: return("警告"); case MessageShowType.Error: case MessageShowType.ErrorOkCancel: return("エラー"); case MessageShowType.Confirmation: case MessageShowType.ConfirmationOkCancel: return("確認"); case MessageShowType.Exception: return("例外"); default: return(""); } }
/// <summary> /// 最前面に来るようにしてMessageBox.Show(text)を呼び出す。 /// </summary> /// <param name="text"></param> public DialogResult MessageShow(string text, MessageShowType type) { var caption = type.Pretty(); var icon = type.ToIcon(); var buttons = type.ToButtons(); if (mainForm != null && mainForm.IsHandleCreated && !mainForm.IsDisposed) { var show = new Func <DialogResult>(() => { return(MessageBox.Show(mainForm, text, caption, buttons, icon)); }); if (mainForm.InvokeRequired) { try { var result = mainForm.BeginInvoke(new Func <DialogResult>(() => { return(show()); })); return((DialogResult)mainForm.EndInvoke(result)); // これで結果が返るまで待つはず.. // ここでウィンドウが破棄される可能性があるのでEndInvoke()が成功するとは限らない。 } catch { return(DialogResult.OK); } } else { return(show()); } } else { return(MessageBox.Show(text, caption, buttons, icon)); } }
/// <summary> /// 最前面に来るようにしてMessageBox.Show(text)を呼び出す。 /// </summary> /// <param name="text"></param> public DialogResult MessageShow(string text, MessageShowType type) { // Linuxでデバッグするときなど、例外が渡ってきて、ウインドウを出せずに終了することがあるので // 例外に関してはメッセージをConsoleに出すほうが親切かもなー。 if (type == MessageShowType.Exception) { Console.WriteLine(text); } var caption = type.Pretty(); var icon = type.ToIcon(); var show = new Func <Form, DialogResult>((parent) => { // MessageBoxのセンタリングにはWindows依存のコードが必要だし、 // Mono環境でMessageBox.Show()で日本語が文字化けするので // 自前で描画する。 // MessageBoxのセンタリングは、メッセージフックしないと不可能。 // cf. // オーナーウィンドウの中央にメッセージボックスを表示する (C#プログラミング) // https://www.ipentec.com/document/csharp-show-message-box-in-center-of-owner-window using (var dialog = new MessageDialog()) { dialog.SetMessage(caption, text, type); if (parent != null) { FormLocationUtility.CenteringToThisForm(dialog, parent); } return(dialog.ShowDialog()); } }); if (mainForm != null && mainForm.IsHandleCreated && !mainForm.IsDisposed) { if (mainForm.InvokeRequired) { try { var result = mainForm.BeginInvoke(new Func <DialogResult>(() => { return(show(mainForm)); })); return((DialogResult)mainForm.EndInvoke(result)); // これで結果が返るまで待つはず.. // ここでウィンドウが破棄される可能性があるのでEndInvoke()が成功するとは限らない。 } catch { return(DialogResult.OK); } } else { return(show(mainForm)); } } else { // メインウインドウ、解体後。 return(show(null)); } }
/// <summary> /// [UI Thread] テキストボックスに表示するメッセージを設定する。 /// </summary> /// <param name="text"></param> public void SetMessage(string caption, string text, MessageShowType type) { Text = caption; // アイコンの設定 var icon = type.ToIcon(); var image_number = -1; switch (icon) { case MessageBoxIcon.Error: image_number = 0; break; case MessageBoxIcon.Question: image_number = 1; break; case MessageBoxIcon.Exclamation: image_number = 2; break; case MessageBoxIcon.Asterisk: image_number = 3; break; } if (image_number >= 0) { var image = TheApp.app.ImageManager.MessageBoxIconImage; var p = pictureBox1; var rect = new Rectangle(image_number * 128, 0, 128, 128); picture_mini = image.CreateAndCopy(p.Width, p.Height, rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb); p.Image = picture_mini.image; } // テキストの設定 if (image_number == 0) { // 例外のときは、テキストボックスに書く。 textBox2.Text = "エラーが発生しました。詳細は以下の通りです。"; textBox1.Text = text; textBox1.SelectionStart = 0; textBox1.SelectionLength = 0; } else { // 例外以外なら、べた書き。 textBox2.Text = text; textBox1.Visible = false; Size = new Size(Width, Height - textBox1.Height + button1.Height / 2); } ShowType = type; Init(); // とりまOKのほうをアクティブにしとく。 button1.Focus(); }
public static MessageBoxButtons ToButtons(this MessageShowType type) { switch (type) { case MessageShowType.Information: case MessageShowType.Warning: case MessageShowType.Error: case MessageShowType.Confirmation: return(MessageBoxButtons.OK); case MessageShowType.InformationOkCancel: case MessageShowType.WarningOkCancel: case MessageShowType.ErrorOkCancel: case MessageShowType.ConfirmationOkCancel: return(MessageBoxButtons.OKCancel); default: return(MessageBoxButtons.OK); } }
/// <summary> /// MessageShowType型を綺麗な日本語文字列に変換 /// </summary> /// <param name="type"></param> /// <returns></returns> public static MessageBoxIcon ToIcon(this MessageShowType type) { switch (type) { case MessageShowType.Information: case MessageShowType.InformationOkCancel: return(MessageBoxIcon.Asterisk); case MessageShowType.Warning: case MessageShowType.WarningOkCancel: return(MessageBoxIcon.Exclamation); case MessageShowType.Error: case MessageShowType.ErrorOkCancel: return(MessageBoxIcon.Hand); case MessageShowType.Confirmation: case MessageShowType.ConfirmationOkCancel: return(MessageBoxIcon.Question); // このIcon、現在、非推奨なのか…。まあいいや。 default: return(MessageBoxIcon.None); } }