/// <summary>
        /// This is used to compute the text bounds for the cell based on the cell bounds, cell style, and the
        /// formatting flags in effect.
        /// </summary>
        /// <param name="cellBounds">The cell bounds</param>
        /// <param name="text">The cell text</param>
        /// <param name="flags">The text formatting flags</param>
        /// <param name="cellStyle">The cell style</param>
        /// <param name="font">The font</param>
        /// <returns>The bounds of the cell's text</returns>
        protected static Rectangle GetTextBounds(Rectangle cellBounds, string text, TextFormatFlags flags,
                                                 DataGridViewCellStyle cellStyle, Font font)
        {
            if ((flags & TextFormatFlags.SingleLine) != TextFormatFlags.GlyphOverhangPadding &&
                TextRenderer.MeasureText(text, font, new Size(Int32.MaxValue, Int32.MaxValue), flags).Width > cellBounds.Width)
            {
                flags |= TextFormatFlags.EndEllipsis;
            }

            Size proposedSize = new Size(cellBounds.Width, cellBounds.Height);
            Size size         = TextRenderer.MeasureText(text, font, proposedSize, flags);

            if (size.Width > proposedSize.Width)
            {
                size.Width = proposedSize.Width;
            }

            if (size.Height > proposedSize.Height)
            {
                size.Height = proposedSize.Height;
            }

            if (size == proposedSize)
            {
                return(cellBounds);
            }

            return(new Rectangle(BaseDataGridViewCell.GetTextLocation(cellBounds, size, flags, cellStyle), size));
        }
Beispiel #2
0
        /// <summary>
        /// Returns the bounding rectangle that encloses the cell's content area, which is calculated using the
        /// specified graphics context and cell style.
        /// </summary>
        /// <param name="graphics">The graphics context</param>
        /// <param name="cellStyle">The cell style to apply to the cell</param>
        /// <param name="rowIndex">The index of the cell's parent row</param>
        /// <returns>The <see cref="Rectangle"/> that bounds the cell's contents</returns>
        /// <remarks>For null cells, a default rectangle is returned so that mouse clicks near its center will
        /// initiate editing.</remarks>
        protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle,
                                                      int rowIndex)
        {
            ImageListColumn owner;
            Rectangle       r = base.GetContentBounds(graphics, cellStyle, rowIndex);
            Size            cellSize, imageSize;

            // For null cells, use a default rectangle so that it enters edit mode if the middle area of the
            // empty cell is clicked.
            if (r == Rectangle.Empty)
            {
                TextFormatFlags flags = BaseDataGridViewCell.ComputeTextFormatFlagsForCellStyleAlignment(
                    base.DataGridView.RightToLeft == RightToLeft.Yes, cellStyle.Alignment, cellStyle.WrapMode);
                cellSize = base.GetSize(rowIndex);

                owner = base.OwningColumn as ImageListColumn;

                if (owner == null || owner.ImageList == null)
                {
                    imageSize = cellSize;
                }
                else
                {
                    imageSize = owner.ImageList.ImageSize;
                }

                r = new Rectangle(new Point(0, 0), cellSize);

                // When stretched, the image covers the whole cell so it doesn't need to be limited
                if (base.ImageLayout != DataGridViewImageCellLayout.Stretch)
                {
                    r = new Rectangle(BaseDataGridViewCell.GetTextLocation(r, imageSize, flags, cellStyle),
                                      imageSize);
                }
            }

            return(r);
        }