Exemplo n.º 1
0
        private void refreshItems()
        {
            DesktopWindow[] currentWindows = DesktopWindow.getCurrentWindows();
            List <int>      currentHandles = currentWindows.Select(w => w.Handle.ToInt32()).ToList();

            foreach (DesktopWindow window in currentWindows)
            {
                ListViewItem item;
                int          handle = window.Handle.ToInt32();

                if (hideWindowInList(window))
                {
                    continue;
                }

                if (currentItems.ContainsKey(handle))
                {
                    item = currentItems[handle];
                }
                else
                {
                    item = new ListViewItem(new string[3]);
                    currentItems.Add(handle, item);
                }

                this.InvokeEx(() =>
                {
                    item.SubItems[0].Text = window.Caption;
                    item.SubItems[1].Text = window.Style.ToString();
                    item.SubItems[2].Text = window.Visible.ToString();
                    item.Tag      = handle;
                    item.ImageKey = window.Visible ? "window-visible" : "window-hidden";
                });
            }

            foreach (KeyValuePair <int, ListViewItem> item in currentItems)
            {
                if (!currentHandles.Contains(item.Key))
                {
                    garbageHandles.Add(item.Key);
                    this.InvokeEx(f => f.windowListView.Items.Remove(item.Value));
                    continue;
                }

                this.InvokeEx(f =>
                {
                    if (!f.windowListView.Items.Contains(item.Value))
                    {
                        f.windowListView.Items.Add(item.Value);
                    }
                });
            }
            garbageHandles.ForEach(gh => currentItems.Remove(gh));
            garbageHandles.Clear();
        }