如何以最佳方式在C#中实现throbber?

3

我想要做的是,在进行长时间加载操作时,使我的System.Windows.Forms.TreeView控件中节点的图标闪烁。

1个回答

4

如果将每个帧加载到ImageList中,您可以使用循环更新到每个帧。示例:

    bool runThrobber = true;
    private void AnimateThrobber(TreeNode animatedNode)
    {
        BackgroundWorker bg = new BackgroundWorker();
        bg.DoWork += new DoWorkEventHandler(delegate
        {
            while (runThrobber)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    animatedNode.SelectedImageIndex++;
                    if (animatedNode.SelectedImageIndex >= imageList1.Images.Count) > animatedNode.SelectedImageIndex = 0;
                });
                Thread.Sleep(100);
            }
        });
        bg.RunWorkerAsync();
    }
显然,实现这个的方法不止一种,但以下是基本思路。

2
再看一遍,你应该在增加索引之前检查并确保图像索引在 imagelist.images 计数的范围内。 - Factor Mystic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接