How do I draw anti-aliased text in a ListBox with DrawMode set to OwnerDrawFixed?

DaveF 21 Reputation points
2024-05-01T18:51:36.1366667+00:00

I have a ListBox on my form with its DrawMode property set to OwnerDrawFixed. How can I get the text I draw to be antialiased? I've already set the TextRenderingHint property of the Graphics object to AntiAliasGridFit but it's not drawn anti-aliased.

This is how the ListBox appears without the TextRendering property set:

User's image

This is how the ListBox appears with the TextRendering property set:

User's image

As you can see, the rendering for both is identical.

Here is the code for my DrawItem event handler:

private void LbColorPalette_DrawItem(object sender, DrawItemEventArgs e)
{
	e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
	e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
	e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

	e.Graphics.FillRectangle(new SolidBrush(m_lbColorPalette.BackColor), e.Bounds);
	e.Graphics.DrawRectangle(new(m_lbColorPalette.BackColor), e.Bounds);

	e.DrawBackground();

	if (e.Index < 0 || e.Index >= m_colorPalette.Count)
	{
		return;
	}

	var color = m_colorPalette[e.Index];
	var swatchSize = e.Bounds.Height - 10;

	e.Graphics.FillRectangle(new SolidBrush(color), e.Bounds.X + 4, e.Bounds.Y + 4, swatchSize, swatchSize);
	e.Graphics.DrawRectangle(new(Color.Black), e.Bounds.X + 4, e.Bounds.Y + 4, swatchSize, swatchSize);

	var text = color.GetColorName();
	var textSize = e.Graphics.MeasureString(text, m_lbColorPalette.Font);
	var textOriginX = e.Bounds.X + swatchSize + 10;
	var textOriginY = e.Bounds.Y + ((e.Bounds.Height - textSize.Height) / 2);

	e.Graphics.DrawString(text, m_lbColorPalette.Font,
		new SolidBrush(((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? Color.White : Color.Black),
		textOriginX, textOriginY);
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 36,471 Reputation points Microsoft Vendor
    2024-05-02T14:46:39.7133333+00:00

    Hi @DaveF , Welcome to Microsoft Q&A,

    I removed the lines that set the CompositingQuality and SmoothingMode and changed the font in the DrawString method to the font in the event parameter. This should ensure that text drawn in the ListBox is anti-aliased.

    private void LbColorPalette_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    
        e.DrawBackground();
    
        if (e.Index < 0 || e.Index >= m_colorPalette.Count)
        {
            return;
        }
    
        var color = m_colorPalette[e.Index];
        var swatchSize = e.Bounds.Height - 10;
    
        e.Graphics.FillRectangle(new SolidBrush(color), e.Bounds.X + 4, e.Bounds.Y + 4, swatchSize, swatchSize);
        e.Graphics.DrawRectangle(new Pen(Color.Black), e.Bounds.X + 4, e.Bounds.Y + 4, swatchSize, swatchSize);
    
        var text = color.GetColorName();
        var textOriginX = e.Bounds.X + swatchSize + 10;
        var textOriginY = e.Bounds.Y + ((e.Bounds.Height - e.Font.Height) / 2);
    
        e.Graphics.DrawString(text, e.Font,
            new SolidBrush(((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? Color.White : Color.Black),
            textOriginX, textOriginY);
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. KOZ6.0 5,215 Reputation points
    2024-05-08T13:39:55.13+00:00

    It seems that the anti-aliasing effect differs depending on the font type.It has no effect if the font is small.

    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            int top = 2;
            int height = 26;
            int titleWidth = 100;
            string[] fontName = { "MS Gothic", "MS UI Gothic", "Arial", "Consolas" };
            TextFormatFlags flags = TextFormatFlags.Left;
            int left = 2;
            for (int i = 0; i < fontName.Length; i++) {
                using (var font = new Font(fontName[i], 12)) {
                    TextRenderer.DrawText(e.Graphics, fontName[i], font, new Rectangle(left, top, titleWidth, height), ForeColor, flags);
                }
                top += height;
            }
            left = 2 + titleWidth;
            for (int pt = 9; pt <= 20; pt++) {
                string text = $"{pt}pt";
                top = 2;
                int maxWidth = 0;
                for (int i = 0; i < fontName.Length; i++) {
                    using (var font = new Font(fontName[i], pt)) {
                        TextRenderer.DrawText(e.Graphics, text, font, new Rectangle(left, top, 100, height), ForeColor, flags);
                        maxWidth = Math.Max(maxWidth, TextRenderer.MeasureText(e.Graphics, text, font).Width);
                    }
                    top += height;
                }
                left += maxWidth;
            }
        }
    }
    

    96dpi enter image description here

    0 comments No comments