【问题标题】:Show "Spinning Wheel” over my Windows Forms application [duplicate]在我的 Windows 窗体应用程序上显示“纺车”[重复]
【发布时间】:2014-03-26 07:06:50
【问题描述】:

我有一个Windows Forms 应用程序,在我的应用程序中,我将文件加载到列表框中,有时这可能需要几秒钟,所以这次我想显示“纺车”,我找到了这个 Gif:http://www.ajaxload.info/ 是否可以在我的应用程序忙于控制器时将其添加到我的应用程序中?

【问题讨论】:

  • 请不要使用 GIF。让系统为你做这件事。
  • 我不能使用 Cursor.Current = Cursors.WaitCursor,只能使用 System.Windows.Forms.DataVisualization.Charting.Cursor
  • 如果你想使用它,你可以使用一个更新进度条/图像的后台工作者。
  • 更新图片是什么意思?
  • @user3271698 我的意思是当你在后台工作时你可以显示一个带有进度条图像的对话框,然后在任务完成时关闭对话框。或者您可以使用默认进度条。

标签: c# winforms


【解决方案1】:

是的

从我拥有的项目中找到了一些旧代码。 编辑了一些东西,你应该可以很容易地让它工作。

调用它:

GuiCursor.WaitCursor(() => { yourclass.DoSomething(); });

班级

internal class GuiCursor
{

    private static GuiCursor instance = new GuiCursor();

    private GuiCursor() { }
    static GuiCursor() { }

    internal static void WaitCursor(MethodInvoker oper)
    {
        if (Form.ActiveForm != null && !Thread.CurrentThread.IsBackground)
        {
            Form myform = Form.ActiveForm;
            myform.Cursor = Cursors.WaitCursor;

            try
            {
                oper();
            }
            finally
            {
                myform.Cursor = Cursors.Default;
            }
        }
        else
        {
            oper();
        }
    }

    internal static void ToggleWaitCursor(Form form, bool wait)
    {
        if (form != null)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new MethodInvoker(() => { form.Cursor = wait? Cursors.WaitCursor : Cursors.Default; }));
            }
            else
            {
                form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default;
            }
        }
    }

    internal static void Run(Form form)
    {
        try
        {
            Application.Run(form);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

根据要求,举个例子。创建一个新的 winform 项目来测试它。 默认情况下,您会得到一个 Form1。给它添加一个按钮,双击它,你会得到一个自动生成的方法。

用这个替换 Form1 类。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GuiCursor.WaitCursor(() => { DoSomething(); });
        }

        private void DoSomething()
        {
            Thread.Sleep(3000);
        }
    }



    internal class GuiCursor
    {

        private static GuiCursor instance = new GuiCursor();

        private GuiCursor() { }
        static GuiCursor() { }

        internal static void WaitCursor(MethodInvoker oper)
        {
            if (Form.ActiveForm != null && !Thread.CurrentThread.IsBackground)
            {
                Form myform = Form.ActiveForm;
                myform.Cursor = Cursors.WaitCursor;

                try
                {
                    oper();
                }
                finally
                {
                    myform.Cursor = Cursors.Default;
                }
            }
            else
            {
                oper();
            }
        }

        internal static void ToggleWaitCursor(Form form, bool wait)
        {
            if (form != null)
            {
                if (form.InvokeRequired)
                {
                    form.Invoke(new MethodInvoker(() => { form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default; }));
                }
                else
                {
                    form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default;
                }
            }
        }

        internal static void Run(Form form)
        {
            try
            {
                Application.Run(form);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

【讨论】:

  • 我把它作为我的项目中的新类,变量 isWaitCursor 无法识别
  • 我把它放在我的应用程序繁忙但什么也没发生的地方
  • 如果你不能让它工作,那么试试这个:void ToggleWaitCursor(Form form, bool wait),发送一个对你的活动表单的引用,如果你想让它显示等待或不显示。然后当你的操作完成后,再做一次告诉它停止显示waitcursor。
  • 你能给我看一个代码示例吗? (我是新开发者...)
  • 我用一个例子更新了上面的帖子。
【解决方案2】:

这样做的一个小技巧可能是使用带有图像的PictureBox。点击按钮时,使PictureBox可见,点击操作完成后再次隐藏。

【讨论】:

  • 我可以使用上面的链接看到这个圆圈在旋转吗?
  • 保存该 GIF 文件并将其放入您的项目中。将此图像用于 PictureBox。
猜你喜欢
  • 2014-03-26
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
相关资源
最近更新 更多