Ejemplo n.º 1
0
        /// <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));
            }
        }
Ejemplo n.º 2
0
        /// <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));
            }
        }