(ListView?)-Control like in Windows Explorer

后端 未结 1 774
情深已故
情深已故 2021-01-24 02:31

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.

1条回答
  •  情话喂你
    2021-01-24 03:24

    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.

    0 讨论(0)
提交回复
热议问题