如何将特定项添加到我的ListBoxItem类?

时间:2016-01-03 04:13:16

标签: c# .net winforms

班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automatic_Record
{
    // <summary>
    /// Class used to represent each item in the listbox.
    /// </summary>
    internal class ListBoxItem : IToolTipDisplayer
    {
        /// <summary>
        /// Text that is displayed in the list box.
        /// </summary>
        public string DisplayText { get; private set; }

        /// <summary>
        /// Text that is displayed in a tooltip.
        /// </summary>
        public string ToolTipText { get; private set; }

        public ListBoxItem(string displayText, string toolTipText)
        {
            DisplayText = displayText;
            ToolTipText = toolTipText;
        }

        /// <summary>
        /// Returns the display text of this item.
        /// </summary>
        /// <returns>Display text of this item.</returns>
        public override string ToString()
        {
            return DisplayText;
        }

        /// <summary>
        /// Returns the tooltip text of this item.
        /// </summary>
        /// <returns>Tooltip text of this item.</returns>
        public string GetToolTipText()
        {
            return ToolTipText;
        }
    }
}

然后在form1的顶部

ListBoxItem[] items = new ListBoxItem[]
            {
                new ListBoxItem("Apple",     "Malus pumila"),
                new ListBoxItem("Banana",    "Porcelia macrocarpa"),
                new ListBoxItem("Kiwi",      "Actinidia deliciosa"),
                new ListBoxItem("Papaya",    "Carica papaya"),
                new ListBoxItem("Mango",     "Mangifera indica"),
                new ListBoxItem("Tomato",    "Lycopersicon esculentum"),
                new ListBoxItem("Lychee",    "Litchi chinensis"),
                new ListBoxItem("Coconut",   "Cocos nucifera"),
                new ListBoxItem("Tangerine", "Citrus reticulata"),
                new ListBoxItem("Avocado",   "Persea americana"),
            };

然后在form1构造函数中:

toolTipListBox1.Items.AddRange(items);

但是添加这些项目我想添加这些项目:

toolTipListBox1.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

问题是我首先需要采取这个数组(ToArray)项目并在ListBoxItem[]项目中为它们制作实例

toolTipListBox1UserControl,我将其推导为ListBox控件。 我正在使用tooltip来显示listBox中每个项目的说明。

在这种情况下,我希望项目WindowSnap.GetAllWindows(true, true).ToArray()也是我想要查看每个项目的说明,因为我不想让listBox尺寸过大。

GetAllWindows方法:

public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
        {
            windowSnaps = new WindowSnapCollection();
            countMinimizedWindows = minimized;//set minimized flag capture
            useSpecialCapturing = specialCapturring;//set specialcapturing flag
            EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
            EnumWindows(callback, IntPtr.Zero);
            return new WindowSnapCollection(windowSnaps.ToArray(), true);
        }

此方法属于类类型:

WindowSnapCollection是:

using System;
using System.Collections.Generic;
using System.Text;

namespace Automatic_Record
{
    class WindowSnapCollection:List<WindowSnap>
    {
        private readonly bool asReadonly = false;

        const string READONLYEXCEPTIONTEXT="The Collection is marked as Read-Only and it cannot be modified";
        private void ThrowAReadonlyException()
        {
            throw new Exception(READONLYEXCEPTIONTEXT);
        }

        public new void Add(WindowSnap item)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Add(item);
        }

        public new void AddRange(IEnumerable<WindowSnap> collection)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.AddRange(collection);
        }

        public new void Clear()
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Clear();
        }

        public new void Insert(int index,WindowSnap item)
        {
                        if (this.asReadonly) this.ThrowAReadonlyException();
            base.Insert(index,item);
        }

        public new void InsertRange(int index, IEnumerable<WindowSnap> collection)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.InsertRange(index, collection);
        }

        public new void Remove(WindowSnap item)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Remove(item);
        }

        public new void RemoveAll(Predicate<WindowSnap> match)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.RemoveAll(match);
        }

        public new void RemoveAt(int index)
        {
                        if (this.asReadonly) this.ThrowAReadonlyException();
            base.RemoveAt(index);
        }

        public new void RemoveRange(int index,int count)
        {
                        if (this.asReadonly) this.ThrowAReadonlyException();
            base.RemoveRange(index,count);
        }

        public new void Reverse(int index, int count)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Reverse(index, count);
        }

        public new void Reverse()
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Reverse();
        }

        public new void Sort()
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Sort();
        }

        public new void Sort(Comparison<WindowSnap> comparison)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Sort(comparison);
        }

        public new void Sort(IComparer<WindowSnap> compare)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Sort(compare);
        }

        public new void Sort(int index,int count,IComparer<WindowSnap> compare)
        {
            if (this.asReadonly) this.ThrowAReadonlyException();
            base.Sort(index,count,compare);
        }


        public bool Contains(IntPtr hWnd)
        {
            if (GetWindowSnap(hWnd) != null) return true;
            return false;

        }

        public WindowSnap GetWindowSnap(IntPtr hWnd)
        {
            checkHWnd = hWnd;
            return base.Find(IshWndPredict);          
        }

        public void Update(WindowSnap item)
        {
            lock (this)
            {
                WindowSnap oldSnap = this.GetWindowSnap(item.Handle);
                this.Remove(oldSnap);
                this.Add(item);
            }
        }

        public WindowSnapCollection GetAllMinimized()
        {
            WindowSnapCollection wsCol= (WindowSnapCollection) base.FindAll(IsMinimizedPredict);
            return wsCol;
        }

        private static bool IsMinimizedPredict(WindowSnap ws)
        {
            if (ws.IsMinimized) return true;
            return false;
        }

        [ThreadStatic]private static IntPtr checkHWnd;
        private static bool IshWndPredict(WindowSnap ws)
        {
            if (ws.Handle == checkHWnd)
                return true;
            return false;
        }

        public bool ReadOnly
        {
            get{return this.asReadonly;}
        }

        public WindowSnapCollection(WindowSnap[] items, bool asReadOnly)
        {
            base.AddRange(items);
            base.TrimExcess();
            this.asReadonly = asReadOnly;
        }

        public WindowSnapCollection()
        {
            this.asReadonly = false;
        }
    }
}

和 WindowSnap类是:

WindowSnap class

这是一个截图我扩展左侧列表框的大小只是为了显示项目是如何添加的。左侧窗口是以下行:

this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

右侧窗口是带有以下行的listBox:

toolTipListBox1.Items.AddRange(WindowSnap.GetAllWindows(true, true).Select(wnd => new ListBoxItem(wnd.Text, wnd.Text)).ToArray());

如何使用ListBoxItem使正确的窗口成为左窗口中的项目? T从顶部开始,没有空行,以显示每个项目窗口文本附近的文本:

并且你也会在每个项目附近显示句柄:....数字

就像左边的listBox一样,右边的是ListBoxItem。

ListBoxes

1 个答案:

答案 0 :(得分:1)

我不知道onCreateViewHolder(),但猜测onBindViewHolder()会返回getItemCount()你可以这样做:

ArrayList

<强>更新

好的,WindowSnap会返回一个GetAllWindows,它应该会实现stringtoolTipListBox1.Items.AddRange(WindowSnap.GetAllWindows(true, true).Select(s => new ListBoxItem(s, s)).ToArray()); 有一个GetAllWindows属性,可以为您提供窗口标题。你能用这个吗?

WindowSnapCollection

更新2:

如果我理解您的上一条评论,您只需要使用IEnumerable<WindowSnap>类的WindowSnap方法。所以就这样做:

Text

toolTipListBox1.Items.AddRange(WindowSnap.GetAllWindows(true, true).Select(wnd => new ListBoxItem(wnd.Text, wnd.Text)).ToArray()); 方法为您提供所需的行:

ToString()