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>
        /// ウインドウのリサイズ時に各Controlのレイアウトを調整する。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MessageDialog_SizeChanged(object sender, System.EventArgs e)
        {
            // SetMessage()での初期化が終わってからしか処理しない。
            if (!initialized)
            {
                return;
            }

            int w  = ClientSize.Width;
            int h  = ClientSize.Height;
            int bh = button1.Height;

            //if (textBox1.Visible)
            // → Show()する前は、Controls.Add(textBox1)しているので、親側のVisibleを反映して常にfalse

            if (!textBox1.Text.Empty())
            {
                textBox1.Size = new Size(w, h - bh - bh / 2 - textBox1.Location.Y);
            }

            // ボタンを表示すべきY座標
            int by = h - bh - bh / 4;

            var buttons = ShowType.ToButtons();

            switch (buttons)
            {
            case MessageBoxButtons.OK:
                button1.Location = new Point(Width / 2 - button1.Width / 2, by);
                button2.Visible  = false;
                break;

            case MessageBoxButtons.OKCancel:
                button1.Location = new Point(Width / 2 - button1.Width - DefaultMargin.Left, by);
                button2.Location = new Point(Width / 2 + DefaultMargin.Left, by);
                break;
            }
        }