在我的Windows窗体应用程序上显示“Spinning Wheel”

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

从我拥有它的项目中找到了一些旧代码。 编辑了一些东西,你应该能够轻松地使它工作。

调用它:

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

class级

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

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