Aug 132008
 

Ever seen that white progress bar in Windows 2000 while it loads and that spinning stick while Linux is being installed. Out of my curiosity I wrote some functions which emulates these progress bars.

You can optimize/customize these functions for your needs, a thank you message will do the trick with me! Take a look at the screen shot in the end to understand the whole behavior.

#include
#include
#include
#include

int main()
{
SetConsoleTitle( “Progress messages demo!” );

// First Linux style progress message
void LinuxStyleProgressMessage( const char* Msg );
LinuxStyleProgressMessage( “This is a linux style progress message…” );

// Read in read character from stream to avoid _kbhit from exiting
_getch();

// Now it’s the turn for Windows 2000 style progress message
std::cout < < "\n\n\nA Windows 2000 style progress message\n\n"; void Windows2000StyleProgressMessage( const char* Msg ); Windows2000StyleProgressMessage( "Installing Windows Vista...\n" ); return 0; } void LinuxStyleProgressMessage( const char* Msg ) { std::cout << Msg; HANDLE hOutputHandle = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO ScrBuffInfo = { 0 }; GetConsoleScreenBufferInfo( hOutputHandle, &ScrBuffInfo ); const char Sticks[] = { '\\',char(179),'/',char(196) }; int Index = 0; while( !_kbhit() ) { std::cout << Sticks[Index]; ++Index; Index %= sizeof(Sticks); Sleep(100); SetConsoleCursorPosition(hOutputHandle,ScrBuffInfo.dwCursorPosition); }// End while }// LinuxStyleProgressMessage void Windows2000StyleProgressMessage( const char* Msg ) { std::cout << Msg; HANDLE hOutputHandle = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO ProgressPos = { 0 }; GetConsoleScreenBufferInfo( hOutputHandle, &ProgressPos ); CONSOLE_SCREEN_BUFFER_INFO MsgPos = ProgressPos; int PercentComplete = 0; int MsgLength = 0; while( !_kbhit() ) { SetConsoleCursorPosition( hOutputHandle, ProgressPos.dwCursorPosition ); for( int MsgIndex = 0; MsgIndex < MsgLength; ++MsgIndex ) { // Insert spaces to delete previous "percentage complete message" std::cout << " "; } SetConsoleCursorPosition( hOutputHandle, ProgressPos.dwCursorPosition ); std::cout << char( 219 ); GetConsoleScreenBufferInfo( hOutputHandle, &ProgressPos ); std::stringstream sstream; sstream << " " << PercentComplete++ << "%"; MsgLength = (int)sstream.str().length(); std::cout << sstream.str(); GetConsoleScreenBufferInfo( hOutputHandle, &MsgPos ); Sleep( rand() % 1000 ); }// End while }// End ShowWin2000StyleProgress[/sourcecode] Here is a snapshot of how this will look, it's not animated but anyway you will have an idea as to what will happen. The stick at the end of the first message keeps rotating. 😉 Of course you can develop different of variations for this.  Second one looks like the progress bar in Windows 2000. As it loads it shows a progress bar kind of thing. It's a non-printable ascii character 219! [caption id="attachment_951" align="alignnone" width="393" caption="A console progress bar demo"]Console progress bars demo[/caption]

Have fun guys! 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.