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
|
/// <summary>
/// Bietet durch Implementation von IDisposable eine kompakte Möglichkeit, den Cursor für eine bestimmte Zeit zu ändern.
/// Der ursprünglich angezeigte Cursor wird automatisch wiederhergestellt
/// </summary>
public class CursorChanger : IDisposable
{
private Cursor rawCursor; // der ursprüngliche Cursor, der später wiederhergestellt wird
private Control control; // das Control für das der Cursor geändert werden soll
/// <summary>
/// Bietet durch Implementation von IDisposable eine kompakte Möglichkeit, den Cursor für eine bestimmte Zeit zu ändern.
/// Der ursprünglich angezeigte Cursor wird automatisch wiederhergestellt.
/// </summary>
/// <param name="cursor">Der Cursor, der gesetzt werden soll.</param>
/// <param name="control">Ein Control zum setzen des Cursors. <remarks>Ist dieses Control in (einem) anderen Control(s) enthalten, wird der Cursor des Root-Controls geändert</remarks></param>
/// <example>
/// public class WaitCursorChanger : CursorChanger
/// {
/// public WaitCursorChanger(Control control) : base(Cursors.WaitCursor,control)
/// { }
/// }
///
/// [...]
/// private void foo()
/// {
/// // Cursor: Default
/// using (new WaitCursorChanger(this))
/// {
/// // Cursor: WaitCursor
/// using (new CursorChanger(Cursors.Help,this))
/// {
/// // Cursor: Help
/// }
/// // Cursor: WaitCursor
/// }
/// // Cursor: Default
/// }
/// </example>
public CursorChanger(Cursor cursor,Control control)
{
this.control = control;
// den gewünschten Cursor setzen
Control rootControl = getRootControl(control);
this.rawCursor = rootControl.Cursor;
rootControl.Cursor = cursor;
}
/// <summary>
/// Ermittelt das Root-Control
/// </summary>
/// <param name="control">Control, dessen Root-Control ermittelt werden soll</param>
/// <returns>das Root-Control des übergebenen Controls</returns>
private Control getRootControl(Control control)
{
return (control.Parent==null) ? control : getRootControl(control.Parent);
}
/// <summary>
/// Freigeben des Objektes
/// </summary>
void IDisposable.Dispose()
{
// den ursprünglichen Cursor wiederherstellen
getRootControl(control).Cursor = rawCursor;
}
}
|