Click here to Skip to main content
15,914,221 members
Home / Discussions / C#
   

C#

 
GeneralRe: control box Pin
Nick Parker4-Jul-04 17:41
protectorNick Parker4-Jul-04 17:41 
GeneralRe: control box Pin
sreejith ss nair4-Jul-04 18:25
sreejith ss nair4-Jul-04 18:25 
QuestionHow can I get the size of the string in pixel ?? Pin
MarkSender4-Jul-04 16:56
MarkSender4-Jul-04 16:56 
AnswerRe: How can I get the size of the string in pixel ?? Pin
Nick Parker4-Jul-04 17:24
protectorNick Parker4-Jul-04 17:24 
Generalclearing console screen Pin
insurgentpyro4-Jul-04 16:42
insurgentpyro4-Jul-04 16:42 
GeneralRe: clearing console screen Pin
Nick Parker4-Jul-04 17:28
protectorNick Parker4-Jul-04 17:28 
GeneralRe: clearing console screen Pin
insurgentpyro4-Jul-04 17:44
insurgentpyro4-Jul-04 17:44 
GeneralRe: clearing console screen Pin
Nick Parker4-Jul-04 18:05
protectorNick Parker4-Jul-04 18:05 
Are you trying to write this as a singleton class? The following works for me:

using System;
using System.Runtime.InteropServices;

namespace Example
{
	class ConsoleClass
	{
		private const int STD_OUTPUT_HANDLE  = -11;
		private const byte EMPTY = 32;
		private int hConsoleHandle;

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

		[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);

		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);
		}
	}

	class Demo
	{
		[STAThread]
		static void Main(string[] args)
		{
			ConsoleClass ClearMyConsole = new ConsoleClass();
			Console.WriteLine("THIS IS FIRST LINE");
			Console.ReadLine();
			ClearMyConsole.Clear();
			Console.WriteLine("THE CONSOLE WAS CLEARED");
			Console.WriteLine("Hit Enter to Terminate");
			Console.ReadLine();
		}
	}
} 


- Nick Parker
My Blog | My Articles

GeneralRe: clearing console screen Pin
insurgentpyro4-Jul-04 18:18
insurgentpyro4-Jul-04 18:18 
GeneralRe: clearing console screen Pin
Nick Parker4-Jul-04 18:25
protectorNick Parker4-Jul-04 18:25 
GeneralRe: clearing console screen Pin
insurgentpyro4-Jul-04 18:35
insurgentpyro4-Jul-04 18:35 
GeneralDrawImage - resize proportionately Pin
myNameIsRon4-Jul-04 16:10
myNameIsRon4-Jul-04 16:10 
GeneralRe: DrawImage - resize proportionately Pin
Nick Parker4-Jul-04 18:17
protectorNick Parker4-Jul-04 18:17 
QuestionHow can I find activeskin skins Pin
Ahmed Gaser4-Jul-04 14:00
Ahmed Gaser4-Jul-04 14:00 
AnswerRe: How can I find activeskin skins Pin
Heath Stewart5-Jul-04 5:45
protectorHeath Stewart5-Jul-04 5:45 
GeneralRe: How can I find activeskin skins Pin
Ahmed Gaser5-Jul-04 11:18
Ahmed Gaser5-Jul-04 11:18 
GeneralSpace in table name problem Pin
benglish724-Jul-04 13:56
benglish724-Jul-04 13:56 
GeneralRe: Space in table name problem Pin
Christian Graus4-Jul-04 14:08
protectorChristian Graus4-Jul-04 14:08 
GeneralRe: Space in table name problem Pin
benglish724-Jul-04 14:20
benglish724-Jul-04 14:20 
GeneralWhoops, it worked! thanks Pin
benglish724-Jul-04 14:27
benglish724-Jul-04 14:27 
GeneralRe: Whoops, it worked! thanks Pin
Christian Graus4-Jul-04 15:04
protectorChristian Graus4-Jul-04 15:04 
GeneralWord 2003 & C# Pin
thesundancekid4-Jul-04 8:10
thesundancekid4-Jul-04 8:10 
GeneralRe: Word 2003 & C# Pin
Heath Stewart5-Jul-04 5:51
protectorHeath Stewart5-Jul-04 5:51 
Generalnoob - using WinConsole - need to compile Pin
insurgentpyro4-Jul-04 6:19
insurgentpyro4-Jul-04 6:19 
GeneralRe: noob - using WinConsole - need to compile Pin
Mike Dimmick4-Jul-04 12:06
Mike Dimmick4-Jul-04 12:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.