3 回答

TA貢獻1793條經(jīng)驗 獲得超6個贊
您可以使用Cursor.Current。
// Set cursor as hourglass
Cursor.Current = Cursors.WaitCursor;
// Execute your time-intensive hashing code here...
// Set cursor as default arrow
Cursor.Current = Cursors.Default;
但是,如果散列操作確實很漫長(MSDN將其定義為超過2-7秒),則可能應使用光標以外的視覺反饋指示器來向用戶通知進度。有關更深入的指導原則,請參閱本文。
編輯:
正如@Am指出的那樣,您可能需要在Application.DoEvents();之后致電Cursor.Current = Cursors.WaitCursor;以確保沙漏實際上已顯示。

TA貢獻1890條經(jīng)驗 獲得超9個贊
其實,
Cursor.Current = Cursors.WaitCursor;
臨時設置“等待”光標,但不能確保在操作結束之前一直顯示“等待”光標。實際上,當您在操作仍在運行時移動鼠標時,其他程序或程序中的控件可以輕松地將光標重置為默認箭頭。
顯示“等待”光標的一種更好的方法是將窗體中的UseWaitCursor屬性設置為true:
form.UseWaitCursor = true;
這將顯示窗體上所有控件的等待光標,直到將此屬性設置為false為止。如果要在應用程序級別上顯示等待光標,則應使用:
Application.UseWaitCursor = true;

TA貢獻1824條經(jīng)驗 獲得超8個贊
在前一個方法的基礎上,我的首選方法(由于這是一個經(jīng)常執(zhí)行的操作)是將等待光標代碼包裝在IDisposable幫助器類中,以便它可以與using()(一行代碼)一起使用,采用可選參數(shù),運行其中的代碼,然后清理(還原光標)。
public class CursorWait : IDisposable
{
public CursorWait(bool appStarting = false, bool applicationCursor = false)
{
// Wait
Cursor.Current = appStarting ? Cursors.AppStarting : Cursors.WaitCursor;
if (applicationCursor) Application.UseWaitCursor = true;
}
public void Dispose()
{
// Reset
Cursor.Current = Cursors.Default;
Application.UseWaitCursor = false;
}
}
用法:
using (new CursorWait())
{
// Perform some code that shows cursor
}
- 3 回答
- 0 關注
- 518 瀏覽
添加回答
舉報