ColorConsole.cs :  » Build-Systems » NETZ » netz » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » NETZ 
NETZ » netz » ColorConsole.cs
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

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.