Dominic B.
Dominic B.

Reputation: 1907

(ListView?)-Control like in Windows Explorer

I'm wondering if there is any way to make a control like that one in windows explorer's auto start when you plugin a device.

enter image description here

I had thought that this could be a listview-control in a more or less modified way, but I was not able to find anything with Google. I also checked many CodeProject-pages.

Does anyone have an idea where I would be able to get the control or how I could make one myself? (I am not that good with OwnerDraw :P)

Thanks.

Upvotes: 1

Views: 857

Answers (1)

TaW
TaW

Reputation: 54453

Actually tweaking a ListView is not any easier than ownerDrawing it. Here is an example that shows, how simple it really is.

You just script one event (DrawItem) and you are done.

This piece of code assumes:

  • The LV's View is set to List
  • You have a suitable ImageList added to your form
  • You have the LV's ownerDraw set to true
  • You have added two columns to hold the text shown in the two labels
  • You have made the 1st column wide enough to hold the whole stuff that gets drawn
  • You have made the LV's FontSize as large as the Images' Height (say 32)
  • Assign the appropriate ImageIndex values to the LV's Items

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
  Point point0 = new Point(e.Bounds.Left, e.Bounds.Top);
  Point point1 = new Point(imageList1.ImageSize.Width + 10, e.Bounds.Top + 5);
  Point point2 = new Point(imageList1.ImageSize.Width + 10, e.Bounds.Top + 25);
  Size size = new Size(listView1.ClientRectangle.Width, e.Bounds.Height);
  Rectangle R = new Rectangle(point0, size);
  Font F1 = new Font(listView1.Font.FontFamily, 11f, FontStyle.Bold);
  Font F2 = new Font(listView1.Font.FontFamily, 10f);

  if (e.Item.Focused) e.Graphics.FillRectangle(Brushes.LightBlue, R);
    else if (e.ItemIndex % 2 == 1) e.Graphics.FillRectangle(Brushes.GhostWhite, R);
  e.Graphics.DrawImage(imageList1.Images[e.Item.ImageIndex], point0 );
  e.Graphics.DrawString(e.Item.Text, F1, Brushes.Black, point1);
  e.Graphics.DrawString(e.Item.SubItems[1].Text, F2, Brushes.Black, point2);
  F1.Dispose(); F2.Dispose();
}

Note that I have hard-coded a few Colors to paint every other line and also the focused item. These colors really should use the respective System colors. These come to mind:

 SolidBrush brush0 = new SolidBrush(SystemColors.ControlLight);
 SolidBrush brush1 = new SolidBrush(SystemColors.Highlight);

I am using the Font that is assigned to the LV but with moderate sizes. Obviously more or less anything, especially the various offsets, can be configured to your liking. But using colors from the System.Colors collection is good way to stay in keeping with your users' Windows themes.

Upvotes: 1

Related Questions