2

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.

4

3 回答 3

1

好吧,“实现相当于 LVIS_CUT 样式”的一种方法如下:

使用沿线的功能

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);    
   }
}

获取 ListView 使用的图像列表(即 listView1.ImageList)并添加所有图标的“剪切”版本。您可以在表单中的 InitializeComponent 之后立即调用它,例如

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

然后你可以使用这样的代码

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;
    }
}

将项目的状态更改为被切割或不被切割。

一旦你得到这个工作,你可以尝试将类似的代码放入 ListView 控件的子类版本中。

于 2009-02-10T18:44:37.827 回答
0

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.

于 2008-12-15T17:09:38.627 回答
0

我将它用于我的文件资源管理器应用程序..

    private void MakeCutItem()
    {
        foreach (ListViewItem item in listView1.SelectedItems)
        {
            Image img = item.ImageList.Images[item.ImageIndex];
            Brush overlay = new SolidBrush(Color.FromArgb(128, BackColor));
            Rectangle rect = new Rectangle(new Point(0, 0), item.ImageList.ImageSize);
            using (Graphics g = Graphics.FromImage(img))
            {
                g.FillRectangle(overlay, rect);
            }
            item.ImageIndex = item.ImageList.Images.Add(img,Color.Empty);
        }
    }
于 2012-02-01T15:46:17.433 回答