ConsoleHelper.cs :  » Development » Command-Line-Option-Parsing » CommandLine » ConsoleUtils » 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 » Development » Command Line Option Parsing 
Command Line Option Parsing » CommandLine » ConsoleUtils » ConsoleHelper.cs
/* This file is part of the CSharpOptParse .NET C# library
 *
 * The library is hosted at http://csharpoptparse.sf.net
 */
using System;
using System.Runtime.InteropServices;

namespace CommandLine.ConsoleUtils{
  #region Structs
  /// <summary>
  /// Position in the console
  /// </summary>
  [StructLayout(LayoutKind.Sequential)]
  public struct ConsolePoint
  {
    /// <summary>
    /// X coordinate
    /// </summary>
    public short X;

    /// <summary>
    /// Y coordinate
    /// </summary>
    public short Y;

    /// <summary>
    /// Constructor
    /// </summary>
    public ConsolePoint(short x, short y)
    {
      X = x;
      Y = y;
    }
  }


  /// <summary>
  /// Rectangle in the console
  /// </summary>
  [StructLayout(LayoutKind.Sequential)]
  public struct ConsoleRect
  {
    /// <summary>
    /// Left edge
    /// </summary>
    public short Left;

    /// <summary>
    /// Top edge
    /// </summary>
    public short Top;

    /// <summary>
    /// Right edge
    /// </summary>
    public short Right;

    /// <summary>
    /// Bottom edge
    /// </summary>
    public short Bottom;
  }


  /// <summary>
  /// Information on the current screen buffer
  /// </summary>
  [StructLayout(LayoutKind.Sequential)]
  public struct ConsoleScreenBufferInfo
  {
    /// <summary>
    /// Size of the screen buffer
    /// </summary>
    public ConsolePoint Size;

    /// <summary>
    /// Position of the cursor on the screen
    /// </summary>
    public ConsolePoint CursorPosition;

    /// <summary>
    /// Attributes
    /// </summary>
    public int          Attributes;

    /// <summary>
    /// Bounds of the window
    /// </summary>
    public ConsoleRect  Window;

    /// <summary>
    /// Maximum window size
    /// </summary>
    public ConsolePoint MaximumWindowSize;
  }
  #endregion Structs

  #region ConsoleHelper class
  /// <summary>
  /// Class to help with more advanced console functions
  /// </summary>
  public class ConsoleHelper
  {
    private const int _stdOutputHandle = -11;
    private int _consoleHandle;

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

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

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

    /// <summary>Cosntructor</summary>
    public ConsoleHelper()
    {
      _consoleHandle = GetStdHandle(_stdOutputHandle);
    }


    /// <summary>
    /// Set the cursor position
    /// </summary>
    public void SetCursorPos(short x, short y)
    {
      SetConsoleCursorPosition(_consoleHandle, new ConsolePoint(x, y));
    }


    /// <summary>
    /// Get the current screen information
    /// </summary>
    public ConsoleScreenBufferInfo GetScreenInfo()
    {
      ConsoleScreenBufferInfo res;
      GetConsoleScreenBufferInfo(_consoleHandle, out res);
      return res;
    }


    /// <summary>
    /// Get the cursor position
    /// </summary>
    public ConsolePoint GetCursorPos()
    {
      ConsoleScreenBufferInfo res;
      GetConsoleScreenBufferInfo(_consoleHandle, out res);
      return res.CursorPosition;
    }
  }
  #endregion ConsoleHelper class
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.