Just one Question?

This is a discussion on Just one Question? within the C++ Programming forums, part of the General Programming Boards category; Ok, I don't know if this exists. But... Does any know of a "clear screen" function that will work in ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    4

    Question Just one Question?

    Ok, I don't know if this exists. But...

    Does any know of a "clear screen" function that will work in a Win32 console App?

    any insight would be great.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Indeed there is, just call clrscr().
    Code:
    void clrscr (void)
    {
      HANDLE hstdout = STDOUT;
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      if (GetConsoleScreenBufferInfo (hstdout, &csbi))
      {
        COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter (hstdout, ' ', dwConSize, coordScreen, &cCharsWritten);
        FillConsoleOutputAttribute (hstdout, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition   (hstdout, coordScreen);
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    //I have a header file I use.. Just name this confx2.h

    /*
    * ConFX2.h (Advanced Textform)
    *
    * Console Effects
    *
    * Original Idea by David Piegrass
    *
    * Version 2.6 created by Johnny Gozde
    * (used original textform function names)
    *
    *
    * Includes:
    * initscreen()
    * clrscr()
    * settitlebar()
    * textcolor()
    * textbackground()
    * temptextcolor()
    * temptextbackground()
    * Highlight()
    * unHighlight()
    * gotxy()
    * HideCursor()
    * ShowCursor()
    * SetTabSize()
    * Error()
    * uninitscreen()
    *
    * Johnny's cOoL commands:
    * SetTitleLine()
    * SetStatusBar()
    * In-Depth Comments
    *
    * Doesn't Include:
    * ConFX class (this version only)
    * Pause() -- Sleep() made this redundant (windows.h)
    * tprintf() -- Redundant because of default color values
    * SetDefColor... -- These commands were blended with textcolor and textbackground
    * SetCurFolMouse()-- Not needed
    * InputLoop() -- Not working fully
    * GetMouseClick() -- Not working fully
    */

    #ifndef ConFXH
    #define ConFXH

    #if !defined(__CONSOLE__) && !defined(_CONSOLE)
    #error Target application must be 32-bit Windows console
    #endif

    #if _MSC_VER > 1000
    #pragma once
    #endif

    #include <stdio.h> // Standard I/O
    #include <conio.h> // Console and port I/O
    #include <stdlib.h> // Needed for _getch()
    #include <windows.h> // Header file for Windows console commands (and others)
    #include <string.h> // Needed for memset()
    //#include "menu.h" // Contains menu paramaters

    /* Not needed because tprintf is not needed

    #include <stdarg.h> // Variable argument functions

    */

    /* Not needed because of the Sleep() command in windows.h

    #include <time.h> // Required for Pause function

    */

    int textpos_x = 0; // Starting horizontal text position
    int textpos_y = 0; // Starting vertical text position
    int tabsize = 5; // Set initial tab size
    int ifColor; // Integer for foreground color
    int ibColor; // Integer for background color

    WORD fColor; // Set foreground color variable
    WORD bColor; // Set background color variable
    WORD Color; // Combination of foreground color and background color

    DWORD cCharsWritten; // Number of characters written

    CONSOLE_SCREEN_BUFFER_INFO csbi; // Buffer info variable

    DWORD dwConWidth; // Width of the console buffer
    DWORD dwConHeight; // Height of the console buffer
    DWORD dwConSize; // Number of character cells in the current buffer

    HANDLE hConsole; // Handle to the console window

    enum efColors // Defines foreground colors
    {
    fgHigh = FOREGROUND_INTENSITY,

    fgBlack = 0,
    fgRed = FOREGROUND_RED,
    fgBlue = FOREGROUND_BLUE,
    fgGreen = FOREGROUND_GREEN,
    fgPurple = fgRed | fgBlue,
    fgYellow = fgRed | fgGreen,
    fgLtGrey = fgRed | fgBlue | fgGreen,
    fgTeal = fgBlue | fgGreen,

    fgBlackHigh = fgBlack | fgHigh,
    fgDkGrey = fgBlackHigh,
    fgRedHigh = fgRed | fgHigh,
    fgBlueHigh = fgBlue | fgHigh,
    fgGreenHigh = fgGreen | fgHigh,
    fgPurpleHigh = fgPurple | fgHigh,
    fgYellowHigh = fgYellow | fgHigh,
    fgWhite = fgLtGrey | fgHigh,
    fgAqua = fgTeal | fgHigh,
    };

    enum ebColors // Defines background colors
    {
    bgHigh = BACKGROUND_INTENSITY,

    bgBlack = 0,
    bgRed = BACKGROUND_RED,
    bgGreen = BACKGROUND_GREEN,
    bgBlue = BACKGROUND_BLUE,
    bgPurple = bgRed | bgBlue,
    bgYellow = bgRed | bgGreen,
    bgLtGrey = bgRed | bgBlue | bgGreen,
    bgTeal = bgBlue | bgGreen,

    bgBlackHigh = bgBlack | bgHigh,
    bgDkGrey = bgBlackHigh,
    bgRedHigh = bgRed | bgHigh,
    bgBlueHigh = bgBlue | bgHigh,
    bgGreenHigh = bgGreen | bgHigh,
    bgPurpleHigh = bgPurple | bgHigh,
    bgYellowHigh = bgYellow | bgHigh,
    bgWhite = bgLtGrey | bgHigh,
    bgAqua = bgTeal | bgHigh,
    };


    char *mCommands[] =
    {
    ""
    };

    enum mCmds
    {
    Temp = 1
    };

    struct HIGHLIGHT
    {
    WORD oldfColor;
    WORD oldbColor;
    short oldX;
    short oldY;
    short oldLength;
    } H;


    // Prototypes

    void initscreen(); // Run this at the start of the program
    void clrscr(); // Clears the console buffer
    void settitlebar(char *title); // Sets the title of the console
    void textcolor(WORD color); // Sets the foreground color
    void textbackground(WORD color); // Sets the background color
    void temptextcolor(WORD color); // Sets temporary colors for tprintf
    void temptextbackground(WORD color); // Sets temporary colors for tprintf
    void Highlight(WORD wfColor, WORD wbColor,
    short nLine, short nStart, short nLength); // Highlights a selected region
    void unHighlight(); // Removes highlighted information
    void gotoxy(short x, short y); // Moves the cursor to a specific location
    void HideCursor(); // Hides the console cursor if visible
    void ShowCursor(); // Shows the console cursor if hidden
    void SetTabSize(int size); // Sets the tab size
    void Error(char error[100]); // Displays an error message
    void uninitscreen(); // Run this at the end of the program
    void tprintf(char *Text, ...); // Prints formatted text

    /* This command is redundant because of the Sleep() command in "windows.h"

    void Pause(double seconds); // Delays for a specified number of seconds

    */

    // Classes

    class cOoLCommands
    {
    public:

    /* Removed to accomodate for loss of tprintf

    void SetDefForeColor(int color); // Sets the default foreground color
    void SetDefBackColor(int color); // Sets the default background color

    */

    void SetTitleLine(char *buffer, short); // Displays a string at the top line of the console
    void SetStatusBar(char *buffer, short); // Displays a string at the bottom line of the console

    /* Removed because they don't fully work... yet
    void SetCurFolMouse(bool bSCFL); // Enables/disables the cursor following the mouse
    bool GetMouseClick(short start, char *string); // Gets a mouse click and returns a char from (start) position
    int InputLoop(); // A do...while loop to get input
    */

    bool bTopLine; // Is there a top line?
    bool bBotLine; // Is there a bottom line?
    // bool bCurFolMouse; // Should the cursor follow the mouse around?

    char *cOldStatus; // Storage for the old status bar line
    short nOldAlign; // Storage for the old alignment

    } CCmd;

    // Functions

    void initscreen() // Run this at the start of the program
    {
    COORD StartCoord = {0,0};

    OSVERSIONINFO osVer; // For GetVersionEx()

    osVer.dwOSVersionInfoSize = sizeof(osVer); // Check if Win32s, if so, display notice and terminate
    GetVersionEx(&osVer);

    if (osVer.dwPlatformId == VER_PLATFORM_WIN32s)
    {
    MessageBox(NULL,
    "This application cannot run on Windows 3.1.\n"
    "This application will now terminate.",
    "Error: Windows NT or Windows 95 Required to Run", MB_OK );
    return;
    }

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Sets the output handle to hConsole

    GetConsoleScreenBufferInfo(hConsole, &csbi); // Get the number of character cells in the current buffer
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y; // Set the variable for the number of character cells
    dwConWidth = csbi.dwSize.X; // Set the variable for the width of the console
    dwConHeight = csbi.dwSize.Y; // Set the variable for the height of the console

    SetConsoleActiveScreenBuffer(hConsole); // Sets the active screen buffer

    SetConsoleCursorPosition(hConsole, StartCoord); // Set the cursor position to 0,0

    fColor = fgLtGrey;
    bColor = bgBlack;

    ifColor = 6;
    ibColor = 0;

    CCmd.bTopLine = false; // There isn't a top line...yet
    CCmd.bBotLine = false; // There isn't a bottom line...yet
    // CCmd.bCurFolMouse = false; // The cursor won't follow the mouse around by default
    }


    void Error(char error[100])
    // Displays an error message
    {
    int oldfColor = ifColor;
    int oldbColor = ibColor;
    textcolor(15);
    textbackground(0);
    printf("%s\n\n", error);
    printf("Press any key to continue");
    _getch();
    textcolor(oldfColor);
    textbackground(oldbColor);
    }


    void clrscr()
    // Clears the console buffer
    {
    COORD StartCoord;

    if (!CCmd.bTopLine)
    {
    StartCoord.X = 0;
    StartCoord.Y = 0;
    }
    else
    {
    StartCoord.X = 0;
    StartCoord.Y = 1;
    }

    FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
    dwConSize, StartCoord, &cCharsWritten);

    if (CCmd.bBotLine)
    CCmd.SetStatusBar(CCmd.cOldStatus, CCmd.nOldAlign);
    }


    void settitlebar(char *title)
    // Sets the title of the console
    {
    SetConsoleTitle(title);
    }


    void textcolor(WORD color)
    // Sets the foreground color
    {
    COORD StartCoord;

    if (!CCmd.bTopLine)
    {
    StartCoord.X = 0;
    StartCoord.Y = 0;
    }
    else
    {
    StartCoord.X = 0;
    StartCoord.Y = 1;
    }

    fColor = (efColors)color;

    Color = fColor | bColor;
    ifColor = color;

    FillConsoleOutputAttribute(hConsole, Color, dwConSize,
    StartCoord, &cCharsWritten);

    SetConsoleTextAttribute(hConsole, Color);
    }


    void textbackground(WORD color)
    // Sets the background color
    {
    COORD StartCoord;

    if (!CCmd.bTopLine)
    {
    StartCoord.X = 0;
    StartCoord.Y = 0;
    }
    else
    {
    StartCoord.X = 0;
    StartCoord.Y = 1;
    }

    bColor = (ebColors)color;

    Color = fColor | bColor;
    ibColor = color;

    FillConsoleOutputAttribute(hConsole, Color, dwConSize,
    StartCoord, &cCharsWritten);

    SetConsoleTextAttribute(hConsole, Color);
    }


    void temptextcolor(WORD color)
    // Sets the foreground color
    {
    COORD StartCoord;

    if (!CCmd.bTopLine)
    {
    StartCoord.X = 0;
    StartCoord.Y = 0;
    }
    else
    {
    StartCoord.X = 0;
    StartCoord.Y = 1;
    }

    fColor = (efColors)color;

    Color = fColor | bColor;
    ifColor = color;

    FillConsoleOutputAttribute(hConsole, Color, dwConSize,
    StartCoord, &cCharsWritten);
    }


    void temptextbackground(WORD color)
    // Sets the background color
    {
    COORD StartCoord;

    if (!CCmd.bTopLine)
    {
    StartCoord.X = 0;
    StartCoord.Y = 0;
    }
    else
    {
    StartCoord.X = 0;
    StartCoord.Y = 1;
    }

    bColor = (ebColors)color;

    Color = fColor | bColor;
    ibColor = color;

    FillConsoleOutputAttribute(hConsole, Color, dwConSize,
    StartCoord, &cCharsWritten);
    }


    void Highlight(WORD wfColor, WORD wbColor,
    short nLine, short nStart, short nLength)
    // Highlights a specified region
    {
    COORD coord;

    if ((nLine < 0) || (nLine > csbi.dwMaximumWindowSize.Y))
    {
    Error("Invalid nLine paramater for Highlight()");
    return;
    }
    if ((CCmd.bTopLine) && (nLine < 1))
    {
    Error("Invalid nLine paramater for Highlight()");
    return;
    }
    if ((CCmd.bBotLine)
    && (nLine > csbi.dwMaximumWindowSize.Y - 1))
    {
    Error("Invalid nLine paramater for Highlight()");
    return;
    }

    if ((nStart < 0) || (nStart > csbi.dwMaximumWindowSize.X))
    {
    Error("Invalid nStart paramater for Highlight()");
    return;
    }

    if (nLength < 1)
    {
    Error("Invalid nLength paramater for Highlight()");
    return;
    }

    if ((nLength + nStart) > csbi.dwMaximumWindowSize.X)
    {
    Error("nStart and nLength are too large (Highlight())");
    }

    coord.X = nStart;
    coord.Y = nLine;

    H.oldbColor = bColor;
    H.oldfColor = fColor;
    H.oldLength = nLength;
    H.oldX = nStart;
    H.oldY = nLine;

    FillConsoleOutputAttribute(hConsole, wfColor |
    wbColor, nLength, coord, &cCharsWritten);
    }


    void unHighlight()
    // Removes highlighted information
    {
    Highlight(H.oldfColor, H.oldbColor, H.oldY, H.oldX,
    H.oldLength);
    }


    void gotoxy(short x, short y)
    // Moves the cursor to a specific location
    {
    if ((x > csbi.dwMaximumWindowSize.X) || (x < 0))
    {
    Error("Invalid x coordinate for gotoxy()");
    return;
    }
    if (!CCmd.bTopLine)
    {
    if ((y > csbi.dwMaximumWindowSize.Y) || (y < 0))
    {
    Error("Invalid y coordinate for gotoxy()");
    return;
    }
    }
    else
    {
    if ((y > csbi.dwMaximumWindowSize.Y) || (y < 2))
    {
    Error("Invalid y coordinate for gotoxy()");
    return;
    }
    }

    COORD coord = {x, y};

    SetConsoleCursorPosition(hConsole, coord);

    textpos_x = x;
    textpos_y = y;
    }


    void tprintf(char *Text, ...)
    // Prints formatted text
    {
    static char buffer[2000];
    COORD coord = {textpos_x, textpos_y};
    COORD coordDest = {0, 0};

    SMALL_RECT srScrollRect;

    CHAR_INFO chiFill;

    va_list arglist;
    va_start(arglist, Text); // Sets up the arglist
    vsprintf(buffer, Text, arglist); // Stores the output into the buffer variable

    for (int i = 0; buffer[i] != '\0'; i++)
    {
    if (buffer[i] == '\a')
    {
    printf ("\a");
    }
    else if (buffer[i] == '\b')
    {
    coord.X--;
    if (coord.X < 0)
    {
    coord.X += 80;
    coord.Y--;
    }
    }
    else if (buffer[i] == '\n')
    {
    coord.X = 0;
    coord.Y++;
    }
    else if (buffer[i] == '\t')
    {
    coord.X = coord.X + tabsize;
    }
    else
    {
    WriteConsoleOutputCharacter (hConsole, &buffer[i],
    1, coord, &cCharsWritten); // Write the character to the screen
    WriteConsoleOutputAttribute (hConsole, &Color,
    1, coord, &cCharsWritten); // Format the character
    coord.X++;

    if (coord.X >= 80) // Move down a line if needed
    {
    coord.X = 0;
    coord.Y++;
    }

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    if ((csbi.dwSize.Y-1) == csbi.dwCursorPosition.Y)
    {
    srScrollRect.Top = 1;
    srScrollRect.Bottom = csbi.dwSize.Y - 1;
    srScrollRect.Left = 0;
    srScrollRect.Right = csbi.dwSize.X - 1;

    coordDest.X = 0;
    coordDest.Y = 0;
    chiFill.Attributes = csbi.wAttributes;
    chiFill.Char.AsciiChar = ' ';

    ScrollConsoleScreenBuffer(hConsole,
    &srScrollRect, NULL, coordDest, &chiFill); // Scroll the screen buffer
    }
    }
    }
    textpos_x = coord.X;
    textpos_y = coord.Y;
    SetConsoleCursorPosition(hConsole, coord);

    va_end(arglist); // Finish using the arglist
    }


    void HideCursor()
    // Hides the console cursor
    {
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hConsole, &cci);
    cci.bVisible = FALSE;
    SetConsoleCursorInfo(hConsole, &cci);
    }


    void ShowCursor()
    // Shows the console cursor
    {
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hConsole, &cci);
    cci.bVisible = TRUE;
    SetConsoleCursorInfo(hConsole, &cci);
    }


    void SetTabSize(int size)
    // Sets the tab size
    {
    tabsize = size;
    }

    void cOoLCommands::SetTitleLine(char *buffer, short align) // Displays a string at the top line of the console
    {
    COORD StartCoord = {0, 0}; // Position the status line at (0, 0)
    COORD NewCoord = {0, 2};
    int len; // The length of the input string parameter
    int left; // Used for calculating how far the string is from the left of the screen
    char szTemp[256];
    short sWidth; // Console width
    sWidth = (short)dwConWidth;

    if (align == 1)
    {
    strcpy(szTemp, buffer); // Add the title
    len = strlen(szTemp); // Get the length of the buffer
    memset(szTemp + len, ' ', sWidth - len); // Blank out rest of line
    }
    else if (align == 2)
    {
    strcpy(szTemp, buffer);
    len = strlen(szTemp); // Get the length
    left = ((sWidth - len) / 2); // Figure out where to place the title
    strcpy(szTemp, " ");
    for (int i = 1; i < left; i++)
    strcat(szTemp, " "); // Fill the left side with spaces
    strcat(szTemp, buffer); // Put the title in
    for (i = 1; i <= (sWidth - len - left); i++)
    strcat(szTemp, " "); // Fill the right side with spaces
    }
    else if (align == 3)
    {
    strcpy(szTemp, buffer);
    len = strlen(szTemp); // Get the length
    left = (sWidth - len); // Get the length of the left side
    strcpy(szTemp, " ");
    for (int i = 1; i < left; i++)
    strcat(szTemp, " "); // Fill the left side with spaces
    strcat(szTemp, buffer); // Now add the title
    }

    WriteConsoleOutputCharacter(hConsole, szTemp, sWidth,
    StartCoord, &cCharsWritten); // Write the string to the console at the correct position

    FillConsoleOutputAttribute(hConsole, fgRed |
    bgWhite, sWidth, StartCoord, &cCharsWritten); // Color the status line so it stands out

    CCmd.bTopLine = true; // There is a top line

    if (csbi.dwCursorPosition.Y < 2)
    SetConsoleCursorPosition(hConsole, NewCoord);
    }


    void cOoLCommands::SetStatusBar(char *buffer, short align) // Displays a string at the bottom line of the console
    {
    COORD StartCoord = {0, (short)dwConHeight -1}; // Position the status line at (0, 0)
    int len; // The length of the input string parameter
    int left; // Used for calculating how far the string is from the left of the screen
    char szTemp[256];
    short sWidth; // Console width
    sWidth = (short)dwConWidth;

    if (align == 1)
    {
    strcpy(szTemp, buffer); // Add the title
    len = strlen(szTemp); // Get the length of the buffer
    memset(szTemp + len, ' ', sWidth - len); // Blank out rest of line
    }
    else if (align == 2)
    {
    strcpy(szTemp, buffer);
    len = strlen(szTemp); // Get the length
    left = ((sWidth - len) / 2); // Figure out where to place the title
    strcpy(szTemp, " ");
    for (int i = 1; i < left; i++)
    strcat(szTemp, " "); // Fill the left side with spaces
    strcat(szTemp, buffer); // Put the title in
    for (i = 1; i <= (sWidth - len - left); i++)
    strcat(szTemp, " "); // Fill the right side with spaces
    }
    else if (align == 3)
    {
    strcpy(szTemp, buffer);
    len = strlen(szTemp); // Get the length
    left = (sWidth - len); // Get the length of the left side
    strcpy(szTemp, " ");
    for (int i = 1; i < left; i++)
    strcat(szTemp, " "); // Fill the left side with spaces
    strcat(szTemp, buffer); // Now add the title
    }

    WriteConsoleOutputCharacter(hConsole, szTemp, sWidth,
    StartCoord, &cCharsWritten); // Write the string to the console at the correct position

    FillConsoleOutputAttribute(hConsole, fgRed |
    bgWhite, sWidth, StartCoord, &cCharsWritten); // Color the status line so it stands out

    CCmd.cOldStatus = buffer; // Set the old status info
    CCmd.nOldAlign = align; // Set the old align info

    CCmd.bBotLine = true; // There is a bottom line
    }

    void uninitscreen() // Run this at the end of the program
    {
    CloseHandle(hConsole); // Closes the handle to the console
    }

    #endif // ConFXH

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    A more common way to do it would be to do:
    Code:
    #include <conio.h>
    //other includes
    ...
    int main ()
    {
       //code here
       clrscr(); //clears screen
       //more code here
       return 0;
    }

  5. #5
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Oh yeah include conio.c instead of .h if you are using Bloodshed Dev C++ (why can't they do .h like everyone else?).

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just on a side note, conio.h isn't a standard header and anything contained in it isn't a standard function so not all compilers will have it.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    4

    Thumbs up Thanks for the Help!

    Thanks, I got to compile and work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21