Пример #1
0
        /// <summary>
        /// Custom implementation of GetPreferredSize that take into account the potential
        /// image or icon and its padding.
        /// </summary>
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (rowIndex != -1)
            {
                throw new ArgumentOutOfRangeException(nameof(rowIndex));
            }

            if (DataGridView == null)
            {
                return(new Size(-1, -1));
            }

            Size basePreferredSize = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);

            if (constraintSize.Width == 0)
            {
                if (_image != null)
                {
                    basePreferredSize.Width += _image.Width + ImagePadding.Horizontal;
                }
                else if (_icon != null)
                {
                    basePreferredSize.Width += _icon.Width + ImagePadding.Horizontal;
                }
            }

            if (constraintSize.Height == 0)
            {
                var dgvabsPlaceholder = new DataGridViewAdvancedBorderStyle();
                DataGridViewAdvancedBorderStyle dgvabsEffective = DataGridView.AdjustColumnHeaderBorderStyle(DataGridView.AdvancedColumnHeadersBorderStyle,
                                                                                                             dgvabsPlaceholder,
                                                                                                             false /*isFirstDisplayedColumn*/,
                                                                                                             false /*isLastVisibleColumn*/);
                Rectangle borderWidthsRect        = BorderWidths(dgvabsEffective);
                int       borderAndPaddingHeights = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
                if (_image != null)
                {
                    basePreferredSize.Height = Math.Max(basePreferredSize.Height, _image.Height + borderAndPaddingHeights + ImagePadding.Vertical);
                }
                else if (_icon != null)
                {
                    basePreferredSize.Height = Math.Max(basePreferredSize.Height, _icon.Height + borderAndPaddingHeights + ImagePadding.Vertical);
                }
            }

            return(basePreferredSize);
        }
Пример #2
0
        /// <summary>
        /// 根据当前单元格边界和单行标题文本的首选单元格高度设置下拉按钮边界值的位置和大小。
        /// </summary>
        private void SetDropDownButtonBounds()
        {
            // 检索单元格显示矩形,用于设置下拉按钮的位置。
            Rectangle cellBounds =
                DataGridView.GetCellDisplayRectangle(
                    ColumnIndex, -1, false);

            // 初始化变量以存储按钮边缘长度,根据字体高度设置其初始值。
            Int32 buttonEdgeLength = InheritedStyle.Font.Height + 5;

            // 计算单元格边框和填充的高度。
            Rectangle borderRect = BorderWidths(
                DataGridView.AdjustColumnHeaderBorderStyle(
                    DataGridView.AdvancedColumnHeadersBorderStyle,
                    new DataGridViewAdvancedBorderStyle(), false, false));
            Int32 borderAndPaddingHeight = 2 +
                                           borderRect.Top + borderRect.Height +
                                           InheritedStyle.Padding.Vertical;
            Boolean visualStylesEnabled =
                Application.RenderWithVisualStyles &&
                DataGridView.EnableHeadersVisualStyles;

            if (visualStylesEnabled)
            {
                borderAndPaddingHeight += 3;
            }

            // 将按钮边缘长度限制为列标题的高度减去边框和填充高度。
            if (buttonEdgeLength >
                DataGridView.ColumnHeadersHeight -
                borderAndPaddingHeight)
            {
                buttonEdgeLength =
                    DataGridView.ColumnHeadersHeight -
                    borderAndPaddingHeight;
            }

            // 将按钮边缘长度约束为单元格减去 3 的宽度。
            if (buttonEdgeLength > cellBounds.Width - 3)
            {
                buttonEdgeLength = cellBounds.Width - 3;
            }

            // 计算下拉按钮的位置,根据是否启用视觉样式进行调整。
            Int32 topOffset  = visualStylesEnabled ? 4 : 1;
            Int32 top        = cellBounds.Bottom - buttonEdgeLength - topOffset;
            Int32 leftOffset = visualStylesEnabled ? 3 : 1;
            Int32 left       = 0;

            if (DataGridView.RightToLeft == RightToLeft.No)
            {
                left = cellBounds.Right - buttonEdgeLength - leftOffset;
            }
            else
            {
                left = cellBounds.Left + leftOffset;
            }

            // 使用计算值设置下拉按钮边界值,并相应地调整单元格填充。
            dropDownButtonBoundsValue = new Rectangle(left, top,
                                                      buttonEdgeLength, buttonEdgeLength);
            AdjustPadding(buttonEdgeLength + leftOffset);
        }
        /// <summary>
        /// Sets the position and size of dropDownButtonBoundsValue based on the current
        /// cell bounds and the preferred cell height for a single line of header text.
        /// </summary>
        private void SetDropDownButtonBounds()
        {
            // Retrieve the cell display rectangle, which is used to
            // set the position of the drop-down button.
            var cellBounds =
                DataGridView.GetCellDisplayRectangle(
                    ColumnIndex, -1, false);

            // Initialize a variable to store the button edge length,
            // setting its initial value based on the font height.
            var buttonEdgeLength = InheritedStyle.Font.Height + 5;

            // Calculate the height of the cell borders and padding.
            var borderRect = BorderWidths(
                DataGridView.AdjustColumnHeaderBorderStyle(
                    DataGridView.AdvancedColumnHeadersBorderStyle,
                    new DataGridViewAdvancedBorderStyle(), false, false));

            var borderAndPaddingHeight = 2 +
                                         borderRect.Top + borderRect.Height +
                                         InheritedStyle.Padding.Vertical;

            var visualStylesEnabled =
                Application.RenderWithVisualStyles &&
                DataGridView.EnableHeadersVisualStyles;

            if (visualStylesEnabled)
            {
                borderAndPaddingHeight += 3;
            }

            // Constrain the button edge length to the height of the
            // column headers minus the border and padding height.
            if (buttonEdgeLength >
                DataGridView.ColumnHeadersHeight -
                borderAndPaddingHeight)
            {
                buttonEdgeLength =
                    DataGridView.ColumnHeadersHeight -
                    borderAndPaddingHeight;
            }

            // Constrain the button edge length to the
            // width of the cell minus three.
            if (buttonEdgeLength > cellBounds.Width - 3)
            {
                buttonEdgeLength = cellBounds.Width - 3;
            }

            // Calculate the location of the drop-down button, with adjustments
            // based on whether visual styles are enabled.
            var topOffset  = visualStylesEnabled ? 4 : 1;
            var top        = cellBounds.Bottom - buttonEdgeLength - topOffset;
            var leftOffset = visualStylesEnabled ? 3 : 1;
            int left;

            if (DataGridView.RightToLeft == RightToLeft.No)
            {
                left = cellBounds.Right - buttonEdgeLength - leftOffset;
            }
            else
            {
                left = cellBounds.Left + leftOffset;
            }

            // Set the dropDownButtonBoundsValue value using the calculated
            // values, and adjust the cell padding accordingly.
            m_dropDownButtonBoundsValue = new Rectangle(left, top,
                                                        buttonEdgeLength, buttonEdgeLength);
            AdjustPadding(buttonEdgeLength + leftOffset);
        }