Jump to content

Clear screen in windows console using API

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
This is mosly useless, I'll admit it, but it's fun :D Perhaps you can learn something from it?

It's software (source and exe) for demonstrating how to clear the screen in the Windows console using Win32 API. It contains a few other things too, like some error handling demonstration and a function for wait (ms) using API.

Check it out, if you think you can learn something/use it for something.

NOTE: If you want to clear the screen without API, try
system("CLS");
But that's cheating, an no fun :D

It was tested on WinXP SP2.

Have fun

Attached Files


Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#2
gamiR

gamiR

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I'm confused !
Hey is it used in c language or in VB 6.0 or in C#?
Which API is used in VB 6.0 to clear the screen?
Busy Penguin

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Dude go read something.

Seriously though, APIs aren't language specific, they are developed for the entire Windows Platform. So any language can potentially take advantage of APIs.

So next time when you are feeling noobish and are tempted to make a post like this, how about you do a google first.

#4
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
API is OS-specific, not language specific, as MeTh0dz said.. This is for clearing cmd.exe in windows
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5
Guest_jgbden_*

Guest_jgbden_*
  • Guests
Thanks, looking forward to checking it out

#6
telboon

telboon

    Newbie

  • Members
  • PipPip
  • 26 posts
Hmm, isn't using Sleep() easier than making the function? Is there an advantage over the Sleep()?

#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Um, I'd say so, as Sleep() does something entirely different than this :)

FYI: Sleep Function (Windows)

As mentioned, you can use system("PAUSE") to let windows clear the screen. My function is way faster though, so if you feel like clearing the screen alot, you might want this. Here is the code, in case people don't want to download it:
int ClrScrn(void)
{
    // Top left corner coordinates
    COORD coord = {0};           

    // Contains information about screen buffer     
    CONSOLE_SCREEN_BUFFER_INFO cBufferInfo;    

    // Characters written in buffer
    DWORD dwI;        
            
    // Characters to clear ("area" of console, so to speak)
    DWORD dwSize;                

    // Handle to console
    HANDLE hI = GetStdHandle(STD_OUTPUT_HANDLE); 
    
    // Just a bit error handling
    if(hI == INVALID_HANDLE_VALUE) return (EXIT_FAILURE); 
    
    // GetConsoleScreenBufferInfo returns 0 on error
    if (GetConsoleScreenBufferInfo(hI, &cBufferInfo))      
    {
    // Calculate characters to clear
    dwSize = cBufferInfo.dwSize.X * cBufferInfo.dwSize.Y;
    
    // Write space characters to buffer as many times as needed, i.e. dwSize    
    FillConsoleOutputCharacter(hI, TEXT(' '), dwSize, coord, &dwI );
    
    // Set console cursor to top left coord
    SetConsoleCursorPosition(hI, coord); 
    
    return (EXIT_SUCCESS);
    
    } else
    return (EXIT_FAILURE);
}

Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#8
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Too... Many... Comments...

#9
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
For clarity :) It's in the tutorial-section, not the code-snippets.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#10
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
There are also many other system commands that can be used for example "color" will change the screens text and background to a specified hexadecimal color. Every command available in the Command Prompt can be turned into a system command.

#11
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
Say you wanted to color only a specific line a specific color, here's an easy method:

enum {

    BLACK       = 0,

    DARK_BLUE   = 1,

    DARK_GREEN  = 2,

    TEAL        = 3,

    DARK_RED    = 4,

    DARK_PURPLE = 5,

    GOLD        = 6,

    GREY        = 7,

    DARK_WHITE  = 8,

    BLUE        = 9,

    GREEN       = 10,

    CYAN        = 11,

    RED         = 12,

    PURPLE      = 13,

    YELLOW      = 14,

    WHITE       = 15

};


inline int SetColor(const int foreground, const int background) {


    int Color = foreground + (background * 16);

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hConsole, Color);


    return 0;

}

An example of usage would be as follows:

#include <iostream>

/* include enumeration list & function */


int main() {


    printf("Text is grey by default\n");

    SetColor(BLACK, WHITE);

    printf("This text is white now!\n");

    SetColor(BLACK, WHITE);

    printf("Now it's black...\n");


    SetColor(GREY, WHITE);

    printf("Now it's back to normal! :(\n");


    cin.get();

    return 0;

}

Welcome.
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills