Console Progress Bar

  • Thread starter Thread starter Ryan Rogers
  • Start date Start date
R

Ryan Rogers

I want to create a progress bar in the console
[# ]
[## ]
etc etc but overwrite itself
how would i go about doing this.. with System.Console.Writeline or Write methods I dont see anyway to reloate the cursor or clear the screen and reprint I would rather just overwrite what I need... any help with this would be great.


Ryan
 
Anwsered My Own Question,

found a nice little Library called ConsoleEx which makes it easer to do the lowlevel console calls that look after the stuff I need.
http://www.gotdotnet.com/Community/...mpleGuid=b76d1f08-2d79-47bd-825b-0489938aae0f

Ryan

I want to create a progress bar in the console
[# ]
[## ]
etc etc but overwrite itself
how would i go about doing this.. with System.Console.Writeline or Write methods I dont see anyway to reloate the cursor or clear the screen and reprint I would rather just overwrite what I need... any help with this would be great.


Ryan
 
Hi Ryan,
You can't get away without using PInvoke unfrotunately. I prepared a small helper class for you. You can used to set and get the console cursor position. Here is some code you can try

using System;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for ClearConsole.
/// </summary><BR/>
public class ConsoleHelper
{
private const int STD_OUTPUT_HANDLE = -11;
private int mHConsoleHandle;

[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
public COORD(short x, short y)
{
X = x;
Y = 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="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, out 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);

/// <summary>Cosntructor</summary>
public ConsoleHelper()
{
//
// TODO: Add constructor logic here.
//
mHConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}

public void SetCursorPos(short x, short y)
{
SetConsoleCursorPosition(mHConsoleHandle, new COORD(x, y));
}

public COORD GetCursorPos()
{
CONSOLE_SCREEN_BUFFER_INFO res;
GetConsoleScreenBufferInfo(mHConsoleHandle, out res);
return res.dwCursorPosition;
}


}

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ConsoleHelper ch = new ConsoleHelper();
//Print some lines
Console.WriteLine("Line 1");
ConsoleHelper.COORD barCoord = ch.GetCursorPos();
//Makes room for the progress bar
Console.WriteLine();
for(int i = 0; i < 80; i++)
{
ConsoleHelper.COORD cur = ch.GetCursorPos();
ch.SetCursorPos((short)(barCoord.X + i), barCoord.Y);
Console.Write('#');
ch.SetCursorPos(cur.X, cur.Y);
Console.WriteLine("I = {0}", i);
Thread.Sleep(1000);
}

Console.ReadLine();
}
}
}


--
HTH
B\rgds
100 [C# MVP]
I want to create a progress bar in the console
[# ]
[## ]
etc etc but overwrite itself
how would i go about doing this.. with System.Console.Writeline or Write methods I dont see anyway to reloate the cursor or clear the screen and reprint I would rather just overwrite what I need... any help with this would be great.


Ryan
 
Back
Top