Пример #1
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView == null)
            {
                return(new Size(-1, -1));
            }

            if (cellStyle == null)
            {
                throw new ArgumentNullException(nameof(cellStyle));
            }

            Size      preferredSize;
            Rectangle borderWidthsRect = StdBorderWidths;
            int       borderAndPaddingWidths = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            int    marginWidths, marginHeights;
            string formattedString = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize) as string;

            if (string.IsNullOrEmpty(formattedString))
            {
                formattedString = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            // Adding space for text padding.
            if (DataGridView.ApplyVisualStylesToInnerCells)
            {
                Rectangle rectThemeMargins = DataGridViewButtonCell.GetThemeMargins(graphics);
                marginWidths  = rectThemeMargins.X + rectThemeMargins.Width;
                marginHeights = rectThemeMargins.Y + rectThemeMargins.Height;
            }
            else
            {
                // Hardcoding 5 for the button borders for now.
                marginWidths = marginHeights = DATAGRIDVIEWBUTTONCELL_textPadding;
            }

            switch (freeDimension)
            {
            case DataGridViewFreeDimension.Width:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1 &&
                    constraintSize.Height - borderAndPaddingHeights - marginHeights - 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin > 0)
                {
                    preferredSize = new Size(
                        MeasureTextWidth(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            constraintSize.Height - borderAndPaddingHeights - marginHeights - 2
                            * DATAGRIDVIEWBUTTONCELL_verticalTextMargin,
                            flags),
                        0);
                }
                else
                {
                    preferredSize = new Size(
                        MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Width,
                        0);
                }
                break;
            }

            case DataGridViewFreeDimension.Height:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1 &&
                    constraintSize.Width - borderAndPaddingWidths - marginWidths - 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin > 0)
                {
                    preferredSize = new Size(
                        0,
                        MeasureTextHeight(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            constraintSize.Width - borderAndPaddingWidths - marginWidths - 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin,
                            flags));
                }
                else
                {
                    preferredSize = new Size(
                        0,
                        MeasureTextSize(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            flags).Height);
                }
                break;
            }

            default:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1)
                {
                    preferredSize = MeasureTextPreferredSize(graphics, formattedString, cellStyle.Font, 5.0F, flags);
                }
                else
                {
                    preferredSize = MeasureTextSize(graphics, formattedString, cellStyle.Font, flags);
                }
                break;
            }
            }

            if (freeDimension != DataGridViewFreeDimension.Height)
            {
                preferredSize.Width += borderAndPaddingWidths + marginWidths + 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Width = Math.Max(preferredSize.Width, borderAndPaddingWidths + IconMarginWidth * 2 + s_iconsWidth);
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Width)
            {
                preferredSize.Height += borderAndPaddingHeights + marginHeights + 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Height = Math.Max(preferredSize.Height, borderAndPaddingHeights + IconMarginHeight * 2 + s_iconsHeight);
                }
            }

            return(preferredSize);
        }