在我的 Windows 窗体应用程序上显示“旋转轮" [英] Show "Spinning Wheel” over my Windows Forms application

查看:22
本文介绍了在我的 Windows 窗体应用程序上显示“旋转轮"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Windows Forms application, in my application i load files into a list box and sometimes this could take few seconds so in this time i want to show "Spinning Wheel" and i found this Gif: http://www.ajaxload.info/ is it possible to add it to my application while my application is busy over the controllers ?

推荐答案

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

Found some old code from a project where I had it. Edited out a few things, you should be able to get it working easily.

调用它:

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.给它添加一个按钮,双击它,你会得到一个自动生成的方法.

As by request, an example. Create a new winform project to test it out. As default you get a Form1. Add a button to it, double click on it so you get a autogenerated method to it.

用这个替换类 Form1.

Replace the class Form1 with this.

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());
            }
        }
    }

这篇关于在我的 Windows 窗体应用程序上显示“旋转轮"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆