簡體   English   中英

表單加載時C#WaitCursor

[英]C# WaitCursor while form loads

我有一個表單,需要幾秒鍾才能最終顯示。 此表格通過以下方式調用:

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

在等待表單最終顯示時,我將默認光標移到Cursors.WaitCursor是很有用的。 目前,我似乎只能通過使用靜態'Current'屬性來成功完成此操作:

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帶寬限制。

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

你應該能夠做到這兩點,沒有問題。

它增加了耦合,因為Cursor.Current = Cursor.Default; 需要在ResultsForm Shown事件中調用

您是否嘗試將光標邏輯完全放入ResultsForm對話框代碼中? 你應該能夠設置Cursor.Current = Cursors.WaitCursor; 在ResultsForm的構造函數內部,然后有Cursor.Current = Cursor.Default; Shown事件中設置。

這將使邏輯完全保留在對話框中,並保持在主窗口之外。 然后,您還可以在主窗口中保持可見性切換。

你為什么要刪除this.Visible = false? 在設置光標時你仍然可以這樣做。

將ResultsForm設置為光標而不是父窗體是否是可接受的解決方案? 讓它在啟動始終占用的代碼之前設置光標,然后在結束時將其設置回來。

如果您總是希望在加載此表單時顯示等待光標(無論是否顯示),那么,正如Reed建議的那樣,您應該只在表單中執行此操作。

但是,如果您只需要在顯示表單的這種特殊情況下使用游標,那么您可以使用lambda(或匿名委托,如果您正在處理C#2.0)來處理事件處理程序:

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

光標將變為WaitCursor,但只能在幾分之一秒內存活。 問題是隱藏主窗體會使光標與主窗體后面的窗口重疊。 或桌面。 顯示的窗口從Windows獲取WM_SETCURSOR消息,它將光標更改為其首選形狀。

但是你的方法存在更大的問題。 對話框關閉后,只有幾分之一秒,您的應用程序中沒有窗口可見。 Windows被迫找到另一個窗口來關注,它不會是你應用程序中的窗口。 當您的主要形式再次變得可見時,它可能最終會在獲得焦點的窗口后面。 這種行為往往有點不穩定,你可能還沒有找到它。

您可以通過以不同方式隱藏窗口來解決這兩個問題:

  this.Opacity = 0.01;

您可以使主窗體成為第二個窗體的所有者並使其成為模態:

frm.ShowDialog(this);

然后在frm中,添加“Cursor.Current = Cursors.WaitCursor;” 在構造函數中添加“Cursor.Current = Cursor.Default;” 所示的事件中。

我用按鈕嘗試了這個,它可以工作。 等待光標顯示,然后在表單加載后返回默認值:

Form1中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM