private void PaintCellText(MapGraphics g, int fontSizeReducedBy, Font legendAutoFont, Size singleWCharacterSize) { bool disposeFont = false; Font cellFont = GetCellFont(legendAutoFont, fontSizeReducedBy, out disposeFont); g.StartHotRegion(this); using (SolidBrush brush = new SolidBrush(GetCellTextColor())) { StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault); stringFormat.FormatFlags = StringFormatFlags.LineLimit; stringFormat.Trimming = StringTrimming.EllipsisCharacter; stringFormat.Alignment = StringAlignment.Center; if (Alignment == ContentAlignment.BottomLeft || Alignment == ContentAlignment.MiddleLeft || Alignment == ContentAlignment.TopLeft) { stringFormat.Alignment = StringAlignment.Near; } else if (Alignment == ContentAlignment.BottomRight || Alignment == ContentAlignment.MiddleRight || Alignment == ContentAlignment.TopRight) { stringFormat.Alignment = StringAlignment.Far; } stringFormat.LineAlignment = StringAlignment.Center; if (Alignment == ContentAlignment.BottomCenter || Alignment == ContentAlignment.BottomLeft || Alignment == ContentAlignment.BottomRight) { stringFormat.LineAlignment = StringAlignment.Far; } else if (Alignment == ContentAlignment.TopCenter || Alignment == ContentAlignment.TopLeft || Alignment == ContentAlignment.TopRight) { stringFormat.LineAlignment = StringAlignment.Near; } SizeF sizeF = g.MeasureStringAbs(GetCellText(), cellFont, new SizeF(10000f, 10000f), stringFormat); if (sizeF.Height > (float)cellPosition.Height && (stringFormat.FormatFlags & StringFormatFlags.LineLimit) != 0) { stringFormat.FormatFlags ^= StringFormatFlags.LineLimit; } else if (sizeF.Height < (float)cellPosition.Height && (stringFormat.FormatFlags & StringFormatFlags.LineLimit) == 0) { stringFormat.FormatFlags |= StringFormatFlags.LineLimit; } g.DrawStringRel(GetCellText(), cellFont, brush, g.GetRelativeRectangle(cellPosition), stringFormat); } g.EndHotRegion(); if (disposeFont) { cellFont.Dispose(); cellFont = null; } }