-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWaitCursor.cs
119 lines (96 loc) · 2.16 KB
/
WaitCursor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace Menees.Windows.Forms
{
#region Using Directives
using System;
using System.Windows.Forms;
#endregion
/// <summary>
/// An object that can be used to specify a "wait" cursor during long operations.
/// </summary>
public sealed class WaitCursor : IDisposable
{
#region Private Data Members
private readonly Cursor? previous;
private readonly Control? control;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance for the specified control.
/// </summary>
/// <param name="control">The control whose Cursor property should be changed.</param>
public WaitCursor(Control? control)
{
this.control = control;
// Try to find the highest-level control (i.e., Form) that we can.
if (this.control != null)
{
Form frm = this.control.FindForm();
if (frm == null)
{
frm = Form.ActiveForm;
}
if (frm != null)
{
this.control = frm;
}
}
else
{
this.control = Form.ActiveForm;
}
if (this.control == null)
{
this.previous = Cursor.Current;
}
else
{
this.previous = this.control.Cursor;
}
this.Refresh();
}
#endregion
#region Public Methods
/// <summary>
/// Closes the cursor and returns it to its previous state.
/// </summary>
public void Close()
{
this.SetCursor(this.previous);
}
/// <summary>
/// Calls <see cref="Close"/>.
/// </summary>
public void Dispose()
{
this.Close();
}
/// <summary>
/// Re-sets the wait cursor.
/// </summary>
public void Refresh()
{
this.SetCursor(Cursors.WaitCursor);
}
/// <summary>
/// Changes the cursor back to the default cursor regardless of nested WaitCursor levels.
/// </summary>
public void ShowDefaultCursor()
{
this.SetCursor(Cursors.Default);
}
#endregion
#region Private Methods
private void SetCursor(Cursor? newCursor)
{
bool useWaitCursor = newCursor is not null && newCursor == Cursors.WaitCursor;
if (this.control != null)
{
this.control.Cursor = newCursor;
this.control.UseWaitCursor = useWaitCursor;
}
Application.UseWaitCursor = useWaitCursor;
Cursor.Current = newCursor;
}
#endregion
}
}