【发布时间】:2014-08-24 00:23:19
【问题描述】:
这就是我刷新和更新 listBox 列表的方式:
private void RefreshWindowsList()
{
ClearGraphics = true;
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
buttonSnap.Enabled = false;
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
buttonSnap.Enabled = true;
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
rectangles = new Rectangle[listBoxSnap.Items.Count];
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
this.listBoxSnap.SetSelected(0, true);
listBoxSnap.Select();
}
我正在清除列表框我正在清除图片框,然后我再次将项目添加到列表框:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
我在 form1 构造函数中调用此方法一次,然后在其他两个地方调用此方法:单击按钮事件和倒计时的计时器滴答事件。
相反,我想更改此方法,以便检查是否有任何新项目或已删除的项目:
WindowSnap.GetAllWindows(true, true).ToArray()
如果有新的窗口(项目)将它们添加到列表框中,如果上次删除了一些项目,则将它们从列表框中删除而不清除列表框,图片框只是根据 WindowSnap 的方式和是否添加/删除项目。 GetAllWindows(true, true).ToArray() 已更改。
所以我可以稍后删除/删除这两行:
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
编辑:
另一个问题是,如果我删除了一个窗口,例如我打开了一个新的 chrome 窗口(不是选项卡,而是窗口),列表框已更新我点击了这个项目并看到它的快照,但是如果我关闭这个窗口我该怎么办?这个item里面有图片的pictureBox怎么会知道现在不显示呢?我不想做:
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
因为它在不停地闪烁。我想以某种方式根据 WindowSnap.GetAllWindows(true, true).ToArray()
的变化来更新 listBox 和 pictureBoxSnap【问题讨论】: