Exemplo n.º 1
0
        public DarkTaskDialog(
            string message,
            string title,
            MessageBoxIcon icon  = MessageBoxIcon.None,
            string?yesText       = null,
            string?noText        = null,
            string?cancelText    = null,
            string?checkBoxText  = null,
            bool?checkBoxChecked = null,
            Button defaultButton = Button.Cancel)
        {
            // All numbers are just matching the original Win32 task dialog as closely as possible. Don't worry
            // about them.

#if DEBUG
            InitializeComponent();
#else
            InitializeComponentSlim();
#endif

            VerificationCheckBox.DarkModeBackColor = DarkColors.Fen_DarkBackground;

            #region Set fonts

            // Set these after InitializeComponent() in case that sets other fonts, but before anything else
            MessageLabel.Font         = SystemFonts.MessageBoxFont;
            YesButton.Font            = SystemFonts.MessageBoxFont;
            NoButton.Font             = SystemFonts.MessageBoxFont;
            Cancel_Button.Font        = SystemFonts.MessageBoxFont;
            VerificationCheckBox.Font = SystemFonts.MessageBoxFont;

            #endregion

            // Have to use these bools, because if we check Visible it will always be false even though we might
            // just have set it to true, because we haven't shown ourselves yet so everything counts as not visible
            bool yesButtonVisible    = yesText != null;
            bool noButtonVisible     = noText != null;
            bool cancelButtonVisible = cancelText != null;
            bool checkBoxVisible     = checkBoxText != null;

            if (!yesButtonVisible && !noButtonVisible && !cancelButtonVisible)
            {
                throw new ArgumentException("At least one button must have text specified!");
            }

            int imageMarginX = icon != MessageBoxIcon.None ? 49 : 7;

            #region Set control state

            base.Text         = title;
            MessageLabel.Text = message;

            if (yesText != null)
            {
                YesButton.Text = yesText;
            }
            if (noText != null)
            {
                NoButton.Text = noText;
            }
            if (cancelText != null)
            {
                Cancel_Button.Text = cancelText;
            }
            if (checkBoxText != null)
            {
                VerificationCheckBox.Text = checkBoxText;
            }

            YesButton.Visible            = yesButtonVisible;
            NoButton.Visible             = noButtonVisible;
            Cancel_Button.Visible        = cancelButtonVisible;
            VerificationCheckBox.Visible = checkBoxVisible;
            VerificationCheckBox.Checked = checkBoxChecked == true;

            if (icon != MessageBoxIcon.None)
            {
                ControlUtils.SetMessageBoxIcon(IconPictureBox, icon);
            }

            MessageLabel.Location = new Point(imageMarginX, MessageLabel.Location.Y);

            #region Set default buttons

            CancelButton = cancelButtonVisible ? Cancel_Button : noButtonVisible ? NoButton : YesButton;
        public MessageBoxCustomForm(
            string messageTop,
            string messageBottom,
            string title,
            MessageBoxIcon icon,
            string okText,
            string cancelText,
            bool okIsDangerous,
            string[]?choiceStrings     = null,
            bool multiSelectionAllowed = true)
        {
#if DEBUG
            InitializeComponent();
#else
            InitializeComponentSlim();
#endif

            _multiChoice = choiceStrings?.Length > 0;

            #region Set fonts

            // Set these after InitializeComponent() in case that sets other fonts, but before anything else
            MessageTopLabel.Font    = SystemFonts.MessageBoxFont;
            MessageBottomLabel.Font = SystemFonts.MessageBoxFont;
            SelectAllButton.Font    = SystemFonts.MessageBoxFont;
            OKButton.Font           = SystemFonts.MessageBoxFont;
            Cancel_Button.Font      = SystemFonts.MessageBoxFont;
            ChoiceListBox.Font      = SystemFonts.DefaultFont;

            #endregion

            #region Set passed-in values

            if (icon != MessageBoxIcon.None)
            {
                ControlUtils.SetMessageBoxIcon(IconPictureBox, icon);
            }

            Text = title;
            MessageTopLabel.Text    = messageTop;
            MessageBottomLabel.Text = messageBottom;

            if (_multiChoice)
            {
                ChoiceListBox.MultiSelect = multiSelectionAllowed;

                ChoiceListBox.BeginUpdate();
                // Set this first: the list is now populated
                for (int i = 0; i < choiceStrings !.Length; i++)
                {
                    ChoiceListBox.Items.Add(choiceStrings[i]);
                }
                ChoiceListBox.EndUpdate();

                if (!multiSelectionAllowed)
                {
                    SelectAllButton.Hide();
                }
            }
            else
            {
                ChoiceListBox.Hide();
                SelectButtonsFLP.Hide();
                MessageBottomLabel.Hide();
            }

            #endregion

            #region Autosize controls

            int innerControlWidth = MainFLP.Width - 10;
            MessageTopLabel.MaximumSize    = new Size(innerControlWidth, MessageTopLabel.MaximumSize.Height);
            MessageBottomLabel.MaximumSize = new Size(innerControlWidth, MessageBottomLabel.MaximumSize.Height);

            if (_multiChoice)
            {
                // Set this second: the list is now sized based on its content
                ChoiceListBox.Size = new Size(innerControlWidth,
                                              (ChoiceListBox.ItemHeight * ChoiceListBox.Items.Count.Clamp(5, 20)) +
                                              (SystemInformation.BorderSize.Height * 2));

                // Set this before window autosizing
                SelectButtonsFLP.Width = innerControlWidth + 1;
            }

            // Set these before setting button text
            if (okIsDangerous)
            {
                OKButton.TextImageRelation = TextImageRelation.ImageBeforeText;
                OKButton.ImageAlign        = ContentAlignment.MiddleCenter;
                OKButton.Image             = Images.RedExclamationMarkCircle;
            }

            // This is here instead of in the localize method because it's not really localizing, it's just setting
            // text that was passed to it. Which in our case actually is localized, but you know.
            OKButton.Text      = okText;
            Cancel_Button.Text = cancelText;

            #endregion

            Localize();

            SelectButtonsFLP.Height = SelectAllButton.Height;

            #region Autosize window

            // Run this after localization, so we have the right button widths

            #region Local functions

            int GetControlFullHeight(Control control, bool onlyIfVisible = false) =>
            !onlyIfVisible || _multiChoice
                    ? control.Margin.Vertical +
            control.Height
                    : 0;