Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / WPF

Zip My Code

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
20 Dec 2009CPOL3 min read 71.6K   2K   48  
A utility stripping your source code to the essential core and then compressing it to a nice CodeProject article attachment.
using System;
using System.Runtime.InteropServices;

namespace netz
{

	public class ColorConsole
	{

#region colors

		public static readonly int FOREGROUND_BLUE =       0x0001;
		public static readonly int FOREGROUND_GREEN =      0x0002;
		public static readonly int FOREGROUND_RED  =       0x0004;
		public static readonly int FOREGROUND_INTENSITY =  0x0008;
		public static readonly int BACKGROUND_BLUE  =      0x0010;
		public static readonly int BACKGROUND_GREEN =      0x0020;
		public static readonly int BACKGROUND_RED   =      0x0040;
		public static readonly int BACKGROUND_INTENSITY =  0x0080;

#endregion colors

#region win32

		private static readonly int STD_OUTPUT_HANDLE = -11;

		[DllImport("Kernel32")]
		private static extern IntPtr GetStdHandle(int nStdHandle);

		[DllImport("Kernel32")]
		private static extern bool SetConsoleTextAttribute(
			IntPtr hConsoleOutput,
			int wAttributes
			);

		[DllImport("Kernel32")]
		private static extern bool GetConsoleScreenBufferInfo(
			IntPtr hConsoleOutput,
			ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
			);

		[StructLayoutAttribute(LayoutKind.Sequential)]
			private struct CONSOLE_SCREEN_BUFFER_INFO 
		{ 
			public int dwSize;
			public int dwCursorPosition;
			public int wAttributes; 
			public long srWindow;
			public long dwMaximumWindowSize;
		}

#endregion win32

		public ColorConsole()
		{
			hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
			if(IsHandleValid())
			{
				CONSOLE_SCREEN_BUFFER_INFO csbiInfo = new CONSOLE_SCREEN_BUFFER_INFO();
				GetConsoleScreenBufferInfo(hStdout, ref csbiInfo);
				attributes = csbiInfo.wAttributes;
			}
		}

		private bool IsHandleValid()
		{
			return !(hStdout.Equals(new IntPtr(-1)));
		}

		public void SetColor(int color)
		{
			if(IsHandleValid())
			{
				SetConsoleTextAttribute(hStdout, color);
			}
		}

		public void Reset()
		{
			SetColor(attributes);
		}

#region state

		private IntPtr hStdout;
		private int attributes = 0;

#endregion state

	}//EOC

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Axis Communications
Sweden Sweden
Got my first computer in the 90's and loved it even though it sounded like a coffeemaker.

Now getting paid for designing cool applications, and drinks the coffee instead of listening to it being made.

Comments and Discussions