在“资源管理器”中,如果一个文件夹共享了,该文件夹下面会出现一个“手”的图标,同样,如果有防火墙,网络连接中“本地连接”会有一个锁的标志,我现在有一个ListView控件和对应的ImageList控件,模式为LargeIcon,ImageList中存放的是ListView用的图标,假如有5个,表示5种情况,显示也很简单,就不赘述了,如myListView.Items[0].ImageIndex=0;什么的,现在有个问题,在某种状态下,需要在该图标上加上另外一个标志,就象“资源管理器”共享时要加一个“手”在下面一样,该如何处理,是否要做10个图标,前面5个是正常的图标,后面5个和前面的完全一样,只是多了“手”形部分?如果这样的话,再多一种情况就更麻烦了,我希望是只要5个图标,然后另外准备一些图标(如“手”、“锁”之类的小图标),在需要的时候,同这5图标进行组合,能实现吗?如何实现?

解决方案 »

  1.   

    当然需要做不同的图标,然后根据状态来显示不同的图。一个ListViewItem只能显示ImageList一张图。
      

  2.   

    byte 转换成 string
    可以使用Encoding.GetString()方法,这里的Encoding需要和写入的一样。byte转换成 int
    可以使用BitConverter.ToInt32方法来转换。
      

  3.   

    需要做成不懂的图标进行组合的话,listview干不了,不过你可以用GDI+自己编程实现图片组合,然后丢给listview
      

  4.   

    我通过编程实现了,首先放两个ImageList,一个是ImgsBase,大小为48*48,存放基本的图标,也就是5个基本状态,再放一个16*16的ImageList,名称为ImgsAttach,用于存放附加的小图标,再编写一个函数,传入基本图标索引和需要组合的附加图标(如果是多个,将状态加起来),函数返回得到的新图标的索引,在该函数中,如果已经有图标了,直接返回索引,如果没有,则动态生成一个,加入到ImgsBase中,并返回新的图标索引,具体示例代码如下:
    第一,定义一个枚举,表示各种附加的情况,注意枚举的值:
            /// <summary>
            /// 几种附加状态
            /// </summary>
            enum AttachState
            {
                有食物 = 1,//二进制的0001
                预订 = 2, //二进制的0010
                保密 = 4  //二进制的0100
            }
    第二、关于如何调用:
     int nTag = 0;//附加状态组合情况,0表示没有附加状态
    if(bHaveFood)//有食物
       nTag+=(int)AttachState.有食物;//当然+= 也可以写成 |=
    if(bPreAsked) //有预订
       nTag += (int)AttachState.预订;
    if (……)//需要保密
        nTag += (int)AttachState.保密;
    //如果还有其他情况,可以参照添加更多的
    this.ListView1.Items[i].GetImageIndex(nBaseImagIndex, nTag);//nBaseImagIndex表示基本的索引
    第三、实现组合图标等其他工作的代码:
     #region 动态生成图标
            /// <summary>
            /// 存放组合后的附加图标信息的嵌套类
            /// </summary>
            internal class AttachIcon
            {
                /// <summary>
                /// 基本图标索引
                /// </summary>
                public int BaseImagIndex=0;
                /// <summary>
                /// 组合标志
                /// </summary>
                public int Tag=0;
                /// <summary>
                /// 组合后图标的索引
                /// </summary>
                public int NewImagIndex=0;
            }
            /// <summary>
            /// 存放附加图标对象的数组
            /// </summary>
            ArrayList m_ArrAttachIcon = new ArrayList();
            /// <summary>
            /// 几种附加状态,上面已经有了
            /// </summary>
            enum AttachState
            {
                有食物 = 1,//二进制的0001
                预订 = 2, //二进制的0010
                保密 = 4  //二进制的0100
            }
            /// <summary>
            /// 根据基本状态和附加状态,返回ImageIndex,如果是基本状态,直接返回,
            /// 否则,将基本状态图标+附加的图标,生成一个新的图标,添加的ImageList中去,
            /// 并返回新的索引
            /// </summary>
            /// <param name="p_BaseImageIndex">基本状态索引</param>
            /// <param name="p_Stat">附加状态的组合</param>
            /// <returns>最终图标的索引</returns>
            private int GetImageIndex(int p_BaseImageIndex, int p_Stat)
            {
                if (p_Stat == 0)
                    return p_BaseImageIndex;
                //查看是已经生成了组合图标,如果有,直接返回索引
                foreach (AttachIcon att in m_ArrAttachIcon)
                {
                    if ((att.BaseImagIndex == p_BaseImageIndex) && (att.Tag == p_Stat))
                        return att.NewImagIndex;
                }
                //重新组合一个出来
                Bitmap bmpBase = (Bitmap)this.imgsBase.Images[p_BaseImageIndex].Clone();
                if ((p_Stat & ((int)AttachState.有食物)) > 0)
                {
                    bmpBase = AttachBmp(bmpBase, (Bitmap)this.imgsAttach.Images[0].Clone(), System.Drawing.ContentAlignment.BottomLeft);
                }
                if ((p_Stat & ((int)AttachState.预订)) > 0)
                {
                    bmpBase = AttachBmp(bmpBase, (Bitmap)this.imgsAttach.Images[1].Clone(), System.Drawing.ContentAlignment.TopLeft);
                }            if ((p_Stat & ((int)AttachState.保密)) > 0)
                {
                    bmpBase = AttachBmp(bmpBase, (Bitmap)this.imgsAttach.Images[2].Clone(), System.Drawing.ContentAlignment.TopRight);
                }            int nIndex = this.imgsBase.Images.Count;
                this.imgsBase.Images.Add(bmpBase);
                AttachIcon newatt = new AttachIcon();
                newatt.BaseImagIndex = p_BaseImageIndex;
                newatt.Tag = p_Stat;
                newatt.NewImagIndex = nIndex;
                this.m_ArrAttachIcon.Add(newatt);
                return nIndex;
            }
            /// <summary>
            /// 将两个位图继续组合,得到一个新的位图
            /// </summary>
            /// <param name="p_BaseBmp">基本位图</param>
            /// <param name="p_AttachBmp">附加位图</param>
            /// <param name="p_Align">附加位图放在基本位图的位置</param>
            /// <returns>生成的新位图</returns>
            private Bitmap AttachBmp(Bitmap p_BaseBmp, Bitmap p_AttachBmp, System.Drawing.ContentAlignment p_Align)
            {
                int nX = 0, nY = 0;
                if (p_Align == ContentAlignment.BottomLeft)
                    nY = p_BaseBmp.Height - p_AttachBmp.Height;
                else if (p_Align == ContentAlignment.BottomRight)
                {
                    nX = p_BaseBmp.Width - p_AttachBmp.Width;
                    nY = p_BaseBmp.Height - p_AttachBmp.Height;
                }
                else if (p_Align == ContentAlignment.TopRight)
                {
                    nX = p_BaseBmp.Width - p_AttachBmp.Width;
                }
                for (int i = 0; i < p_AttachBmp.Height; i++)
                {
                    for (int j = 0; j < p_AttachBmp.Width; j++)
                    {
                        Color cr = p_AttachBmp.GetPixel(j, i);
                        if (cr.ToArgb() != Color.White.ToArgb())
                            p_BaseBmp.SetPixel(nX + j, nY + i, p_AttachBmp.GetPixel(j, i));
                    }
                }
                return p_BaseBmp;
            }
            #endregion
      

  5.   

    this.ListView1.Items[i].GetImageIndex(nBaseImagIndex, nTag);//nBaseImagIndex表示基本的索引
    ================
    这句话弄错了,差了点东西ImageIndex,正确的是:
    this.ListView1.Items[i].ImageIndex=GetImageIndex(nBaseImagIndex, nTag);//nBaseImagIndex表示基本的索引大家来接分吧