4

我有一个需要几秒钟才能最终显示的表单。此表单通过以下方式调用:

using (ResultsForm frm = new ResultsForm())
{
    this.Visible = false;
    frm.ShowDialog();
    this.Visible = true;
}

Cursors.WaitCursor在等待表单最终显示时获得默认光标非常有用。目前,我似乎只能通过使用静态“当前”属性成功地做到这一点:

using (ResultsForm frm = new ResultsForm())
{
    //this.Visible = false;
    Cursor.Current = Cursors.WaitCursor;
    frm.ShowDialog();
    //this.Visible = true;
}

但这有两个问题:

  • 它迫使我禁用我想保留的 MainForm 隐藏功能。
  • 它增加了耦合,因为Cursor.Current = Cursor.Default;需要在 ResultsForm Shown 事件中调用。

如何在表单加载时更改光标而不更改第一个代码段并避免耦合?

更新: 现在问题得到了回答,视频演示被删除了,所以我不会超过我的 ISP 带宽限制。

4

5 回答 5

2

它迫使我禁用我想保留的 MainForm 隐藏功能。

你应该能够做到这两点,没有问题。

由于 Cursor.Current = Cursor.Default; 它增加了耦合。需要在 ResultsForm Shown 事件中调用

您是否尝试过将光标逻辑完全放入 ResultsForm 对话框代码中?您应该能够Cursor.Current = Cursors.WaitCursor;在 ResultsForm 的构造函数中进行设置,然后Cursor.Current = Cursor.Default;Shown事件中进行设置。

这会将逻辑完全保留在对话框中,并且不在主窗口中。然后,您还可以在主窗口中保持可见性切换。

于 2009-11-30T21:51:57.300 回答
1

为什么一定要去掉this.Visible = false?您应该仍然可以在设置光标时执行此操作。

让 ResultsForm 设置光标而不是父表单是一个可接受的解决方案吗?让它在它开始花费所有时间的代码之前设置光标,然后在最后设置它。

于 2009-11-30T21:51:39.190 回答
1

If you always want to display a wait cursor when loading this form (regardless of whether it's shown from), then, as Reed suggests, you should just do it in the form itself.

However, if you only need the cursor in this one particular case of displaying a form, then you can use a lambda (or anonymous delegate, if you're dealing with C# 2.0) for an event handler:

using (ResultsForm frm = new ResultsForm())
{
    this.Visible = false;

    var oldCursor = Cursor.Current;
    Action<object, EventArgs> restoreCursor = 
        delegate
        {
            Cursor.Current = oldCursor;
            frm.Shown -= restoreCursor;
        };
    frm.Shown += restoreCursor;

    Cursor.Current  = Cursor.WaitCursor
    frm.ShowDialog();
}
于 2009-11-30T21:58:37.973 回答
1

光标将变为 WaitCursor ,但只会存活几分之一秒。问题是隐藏主窗体会使光标与主窗体后面的窗口重叠。或者桌面。显示的窗口从 Windows 获得 WM_SETCURSOR 消息,它将光标更改为首选形状。

但是你的方法有一个更大的问题。对话框关闭后,有几分之一秒的时间,您的应用程序中没有任何窗口可见。Windows 被迫寻找另一个窗口来获得焦点,它不会是您应用程序中的窗口。当您的主窗体再次变得可见时,它很可能最终出现在获得焦点的窗口后面。这种行为往往有点不稳定,您可能还没有发现。

您可以通过以不同的方式隐藏窗口来解决这两个问题:

  this.Opacity = 0.01;
于 2009-11-30T23:00:07.617 回答
0

您可以使主窗体成为第二个窗体的所有者并使其成为模态:

frm.ShowDialog(this);

然后在frm中,添加“Cursor.Current = Cursors.WaitCursor;” 在构造函数中添加“Cursor.Current = Cursor.Default;” 在显示事件中。

我已经用一个按钮试过了,它可以工作。等待光标显示,然后在表单加载后返回默认值:

表格1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

      private void ButtonClick(object sender, EventArgs e)
      {
         using(Form2 form2 = new Form2())
         {
            form2.ShowDialog(this);
         }
      }
   }
}

表格2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WaitCursorTwoForms
{
   public partial class Form2 : Form
   {
      public Form2()
      {
         Cursor.Current = Cursors.WaitCursor;
         InitializeComponent();
         for (int i = 0; i < 10; i++)
         {
            Thread.Sleep(1000);
         }
      }

      private void OnShown(object sender, EventArgs e)
      {         
         Cursor.Current = Cursors.Default;
      }
   }
}
于 2009-11-30T22:01:11.277 回答