dotnet-snippets.de
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1314 | Anzahl registrierter User: 1266 | Besucher online: 17
Hauptmenü
Home
Snippet Wettbewerb
Top Ten
Zufälliger Snippet
Vista Gadget
T-Shirts für Geeks
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social
Facebook
Twitter
RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss Delphi.NET
Rss ASP.NET
Werbung
Alice DSL Flatrate

Partner
Partner von Codezone.de


Member of Microsoft Community Leader/Insider Program (CLIP)

CursorChanger


Autor: dN!3L
Sprache: C#
Bewertung: 4,69
(3 Bewertungen)

Anzahl der Aufrufe: 5455
  

Beschreibung:

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.


Beispiel:
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
}


Abgelegt unter: Cursor, WaitCursor, Cursors, Control.



C#
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;
	}
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.