2

I have a problem with function "listView1_DrawSubItem". I change only second column, because I have to put some image in second column. Problem is with FONT. When I draw second column font is more sharp than in first column. What is amazing it appears only when I first open Chart form. As it is show in code first column is drawinng by default, second column is drawing by me.

There is an image of this. Watch it on full resolution. With chart, without chart

Here is my code:

fo is my Font which I can change.

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        if (e.Header != this.columnHeader2)
        {
            e.DrawDefault = true;
            return;
        }

        if (e.Item.SubItems[1].Text == "1")
        {
            e.DrawBackground();
            e.Graphics.DrawImage(Properties.Resources.Blank_Badge_Green, e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y, 10, 10);
        }
        else if (e.Item.SubItems[1].Text == "0")
        {
            e.DrawBackground();
            e.Graphics.DrawImage(Properties.Resources.Blank_Badge_Grey, e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y, 10, 10);
        }
        else
        {
            e.DrawBackground();
            e.Graphics.DrawString(e.SubItem.Text, fo, new SolidBrush(e.SubItem.ForeColor), e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y);
        }
    }

    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        e.DrawDefault = true;
    }

2 Answers2

4
   e.Graphics.DrawString(...)

Two problems. First one is the method you use, ListView uses TextRenderer.DrawText() under the hood itself. The second problem is evident when you use a utility like SysInternals' ZoomIt (recommended), you'll see that the nasty looking text is rendered without the blue/red anti-aliasing pixels. You'll need to set the Graphics.TextRenderingHint property to avoid this.

So, roughly:

else
{
    e.DrawBackground();
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    TextRenderer.DrawText(e.Graphics, ...);
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • One more Question about Treeview. It flickering when I update and change checkbox. I use property DoubleBuffered alredy: "PropertyInfo property1 = typeof(TreeView).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance); property1.SetValue(treeView1, true, null);" – user3447900 Oct 14 '14 at 07:13
  • ok I found it in [TreeView Flickering](http://stackoverflow.com/questions/10362988/treeview-flickering) – user3447900 Oct 14 '14 at 07:51
0

Most likely you have to test the various SmoothingModes of the Graphics you draw on:

    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

Try to see which matches best the quality of the system drawn cells!

Set it right before you draw the text!

In theory a few other properties may make a difference:

int e.Graphics.TextContrast    // for adding a gamma correction
e.Graphics.InterpolationMode   // for resizing images
e.Graphics.CompositingMode     // for combining an image with the pixels below
e.Graphics.CompositingQuality  // controls the quality thereof

But most likely it is the SmoothingMode.

TaW
  • 53,122
  • 8
  • 69
  • 111