Note that there are some explanatory texts on larger screens.

plurals
  1. POListView OwnerDraw artifacting when using LargeIcon view
    text
    copied!<p>I have a ListView in a C# WinForms project with <code>OwnerDraw</code> set to true. I'm populating both LargeIcon and List views, as well as the <code>LargeImageList</code> and <code>SmallImageList</code> properties (both of which have only a single image as all items display the same icon).</p> <p>List view is drawing without issue:</p> <p><img src="https://i.stack.imgur.com/pxKoQ.png" alt="ListView in View mode"></p> <p>LargeIcon view displays correctly initially:</p> <p><img src="https://i.stack.imgur.com/wDYWR.png" alt="ListView in LargeIcon mode, nothing selected"></p> <p>but leaves background artifacts as the selected item changes (doesn't matter if clicking or using the arrow keys):</p> <p><img src="https://i.stack.imgur.com/UjmQK.png" alt="enter image description here"></p> <p>Also, as shown, there's an issue with the text being cut off if too long, but that's a secondary concern.</p> <p>Here is my DrawItem event (<code>ORANGE</code> and <code>WHIE</code> are color constant values declared elsewhere):</p> <pre><code>private void ListView_DrawItem( object sender, DrawListViewItemEventArgs e ) { ListView list = sender as ListView; if( e.Item.Selected ) { e.Graphics.FillRectangle( new SolidBrush( ORANGE ), e.Bounds ); } else { e.Graphics.FillRectangle( new SolidBrush( WHITE ), new Rectangle( e.Bounds.Location, e.Bounds.Size ) ); } e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; if( list.View == View.List ) { e.Graphics.DrawImage( list.SmallImageList.Images[0], new Point( e.Bounds.Left + 4, e.Bounds.Top ) ); e.Graphics.DrawString( e.Item.Text, new Font( "SegoeUI", 10, FontStyle.Regular ), new SolidBrush( Color.Black ), new PointF( e.Bounds.Left + 22, e.Bounds.Top + 1 ) ); } else if( list.View == View.LargeIcon ) { e.Graphics.DrawImage( list.LargeImageList.Images[0], new Point( e.Bounds.Left + ( ( e.Bounds.Width - 32 ) / 2 ), e.Bounds.Top ) ); e.Graphics.DrawString( e.Item.Text, new Font( "SegoeUI", 10, FontStyle.Regular ), new SolidBrush( Color.Black ), new RectangleF( new PointF( e.Bounds.Left, e.Bounds.Top + 34 ), new SizeF( e.Bounds.Width, e.Bounds.Height - 34 ) ), new StringFormat { Alignment = StringAlignment.Center } ); } } </code></pre> <p>A lot of this has been trial and error, including the geometry calculations and using the <code>TextRenderingHint</code>, which I did to get font smoothing, but I'm not sure I'm using the right value.</p> <p>I last did an owner-drawn ListView years ago, but I guess I'm rusty now, and for the life of me can't make it work. Any pointers would be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload