public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultbutton)
        {
            DialogResult result = DialogResult.None;

            Form ownerForm = null;

            if (owner != null)
            {
                ownerForm = (Form)owner;
            }
            else if (Form.ActiveForm != null)
            {
                ownerForm = Form.ActiveForm;
            }

            switch (icon)
            {
            case MessageBoxIcon.Error:
                SystemSounds.Hand.Play();
                break;

            case MessageBoxIcon.Exclamation:
                SystemSounds.Exclamation.Play();
                break;

            case MessageBoxIcon.Question:
                SystemSounds.Beep.Play();
                break;

            default:
                SystemSounds.Asterisk.Play();
                break;
            }

            ModernMessageBoxForm modernMessageBoxControl = new ModernMessageBoxForm();

            if (ownerForm != null && ownerForm.WindowState != FormWindowState.Minimized)
            {
                modernMessageBoxControl.BackColor = ownerForm.BackColor;
                modernMessageBoxControl.Size      = new Size(ownerForm.Size.Width, modernMessageBoxControl.Height);
                modernMessageBoxControl.Location  = new Point(ownerForm.Location.X, ownerForm.Location.Y + ((ownerForm.Height - modernMessageBoxControl.Height) / 2));
            }
            else
            {
                IntPtr currentProcessHandle = Process.GetCurrentProcess().MainWindowHandle;
                Screen currentProcessScreen = Screen.FromHandle(currentProcessHandle);

                modernMessageBoxControl.Size          = new Size(currentProcessScreen.WorkingArea.Width, (currentProcessScreen.WorkingArea.Height - modernMessageBoxControl.Height) / 2);
                modernMessageBoxControl.StartPosition = FormStartPosition.CenterScreen;
            }

            modernMessageBoxControl.Properties.Buttons       = buttons;
            modernMessageBoxControl.Properties.DefaultButton = defaultbutton;
            modernMessageBoxControl.Properties.Icon          = icon;
            modernMessageBoxControl.Properties.Text          = text;
            modernMessageBoxControl.Properties.Caption       = caption;
            modernMessageBoxControl.Padding       = new Padding(0, 0, 0, 0);
            modernMessageBoxControl.ControlBox    = false;
            modernMessageBoxControl.ShowInTaskbar = false;
            modernMessageBoxControl.ArrangeAppearance();
            modernMessageBoxControl.TopLevel = true;
            modernMessageBoxControl.TopMost  = true;
            modernMessageBoxControl.ShowDialog();
            modernMessageBoxControl.BringToFront();
            modernMessageBoxControl.SetDefaultButton();

            Action <ModernMessageBoxForm> action = new Action <ModernMessageBoxForm>(ModalState);
            IAsyncResult asyncResult             = action.BeginInvoke(modernMessageBoxControl, null, action);
            bool         cancelled = false;

            try
            {
                while (!asyncResult.IsCompleted)
                {
                    Thread.Sleep(1);
                    Application.DoEvents();
                }
            }
            catch
            {
                cancelled = true;

                if (!asyncResult.IsCompleted)
                {
                    try
                    {
                        asyncResult = null;
                    }
                    catch
                    {
                    }
                }

                action = null;
            }

            if (!cancelled)
            {
                result = modernMessageBoxControl.Result;
                modernMessageBoxControl.Dispose();
                modernMessageBoxControl = null;
            }

            return(result);
        }
 /// <summary>
 /// ModalState method.
 /// </summary>
 /// <param name="control">ModernMessageBoxControl instance.</param>
 private static void ModalState(ModernMessageBoxForm control)
 {
     while (control.Visible)
     {
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ModernMessageBoxProperties" /> class.
 /// </summary>
 /// <param name="owner">ModernMessageBoxControl instance.</param>
 public ModernMessageBoxProperties(ModernMessageBoxForm owner)
 {
     this.Owner = owner;
 }