Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1839 | Besucher online: 7
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Console löschen


Autor: Jan Welker
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 6648
  
Kick it on dotnet-kicks.de  

Beschreibung:

Löscht den kompletten Inhalt bei einer Consolenanwendung im .NET Framework 1.1

Abgelegt unter: löscht, inhalt, löschen, console, cls, clear, screen.



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
using System;
using System.Runtime.InteropServices;

// Klasse zum löschen des Konsoleninhalt 
public class ClearConsole
{		
  private const int STD_OUTPUT_HANDLE  = -11;
  private const byte EMPTY = 32;

  [StructLayout(LayoutKind.Sequential)]
    struct COORD
  {
    public short x;
    public short y;
  }

  [StructLayout(LayoutKind.Sequential)]
    struct SMALL_RECT
  {
    public short Left;
    public short Top;
    public short Right;
    public short Bottom;
  }
	
  [StructLayout(LayoutKind.Sequential)]
    struct	CONSOLE_SCREEN_BUFFER_INFO
  {
    public COORD dwSize;
    public COORD dwCursorPosition;
    public int wAttributes;
    public SMALL_RECT srWindow;
    public COORD dwMaximumWindowSize;
  }

  [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  private static extern int GetStdHandle(int nStdHandle);

  [DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);

  [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

  [DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);

  private int hConsoleHandle;

  public ClearConsole()
  {
    hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  }

  public void Clear()
  {
    int hWrittenChars = 0;
    CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();			
    COORD Home;		
    Home.x = Home.y = 0;
    GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
    FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
    SetConsoleCursorPosition(hConsoleHandle, Home);
  }
}

// Beispielafruf:
// ClearConsole clearCon = new ClearConsole();
// clearCon.Clear();
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.)

Jan Welker schrieb am:  14.07.2006 19:17:30

Im .NET Framework 2.0 funktioniert auch
Console.Clear()
herbivore schrieb am:  10.12.2006 00:30:52

Wenn man wirklich Spaß mit der Console haben möchte, sollte man .NET 2.0 verwenden. Dort gibt es nicht nur die Clear-Methode, sondern auch viele andere Features, die man in .NET 1.x schmerzlich vermisst.
Jan Welker schrieb am:  20.01.2007 01:40:28

ja, zum Beispiel Farbe :-)


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