views:

558

answers:

2

Hi All,

How do I achieve the equivalent of the LVIS_CUT style for a listview item? It looks like it's not exposed by the framework? Should I P/Invoke?

Edit: LVIS_CUT is a Win32 style that affects the look of the item: It grays the item image. You can see it in action in Windows Explorer: Select a file and type Ctrl+X.

TIA.

A: 

Are you referring to when it's grayed out? Like when you do a "cut" on it? If so, I would just set the forecolor to Inactive or something along those lines. Not sure that you need to pinvoke for something like that.

BFree
Yes. But it also grays the item image. Do you have an easy solution for this one?
Serge - appTranslator
+1  A: 

Well, one way to "achieve the equivalent of the LVIS_CUT style" would be the following:

Use a function along the lines of

private void MakeCutList(ImageList sourceList, Color background)
{
   Brush overlay = new SolidBrush(Color.FromArgb(128, BackColor));
   Rectangle rect = new Rectangle(new Point(0, 0), sourceList.ImageSize);

   foreach (Image img in sourceList.Images)
   {
      Bitmap cutBmp = new Bitmap(img.Width, img.Height);

      using (Graphics g = Graphics.FromImage(cutBmp))
      {
         g.DrawImage(img, 0, 0);
         g.FillRectangle(overlay, rect);
      }

      sourceList.Images.Add(cutBmp);    
   }
}

to take the image list used by your ListView (i.e. listView1.ImageList) and add the "cut" versions of all the icons. You might call this immediately after InitializeComponent in your form, like

public Form1()
{
    InitializeComponent();
    MakeCutList(listView1.LargeImageList, listView1.BackColor);
}

Then you could use code like this

private void SetCutState(ListViewItem lvi, Boolean isItemCut)
{
    int originalListSize = lvi.ImageList.Images.Count / 2;
    int baseIndex = lvi.ImageIndex % originalListSize;
    int cutImagesOffset = originalListSize;

    if (isItemCut)
    {
        lvi.ImageIndex = cutImagesOffset + baseIndex;
        lvi.ForeColor = SystemColors.GrayText;
    }
    else
    {
        lvi.ImageIndex = baseIndex;
        lvi.ForeColor = SystemColors.WindowText;
    }
}

to change the state of an item to being cut or not.

Once you get that working, you could try to put similar code into a subclassed version of the ListView control.

Daniel LeCheminant
Interesting. Thanks!
Serge - appTranslator