Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,568 people online right now. Registration is fast and FREE... Join Now!




Hold display specific area after clrsrc() ?

 
Reply to this topicStart new topic

Hold display specific area after clrsrc() ?, my program clear screen all area above from display

imamkomc
3 Nov, 2007 - 07:09 AM
Post #1

D.I.C Head
Group Icon

Joined: 9 May, 2007
Posts: 62


Dream Kudos: 225
My Contributions
Hi All,

I want to use clrscr() common to clear screen only specific area.
but my program clear screen all area above from display.
Running program:
test number :
=================
input number : 4
<warning>, input number must odd

because i am give sleep(2), than "<warning>, input number must odd" will appear in 2 second. And clrscr(), display on screen become,
input number :

How to hold display like :
test number :
=================
input number :

This my code :
CODE

.

class x {
     public :
     void y();
};

void x::y()
{
int n;
      start:
      cout<<"input number:";  
      cin>>n;
      if (n%2==0){
         cout<<"<warning>, input number must odd\n";
         sleep(2);
         clrscr();
           goto start;
      }
}

void main (){
x run;
     cout<<"\ntest number :\n";
     cout<<"\n===================\n";
     run.y();
getch();
}
.

Any body help me.
Thanks before,

ImamkomC++.
Regard.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hold Display Specific Area After Clrsrc() ?
3 Nov, 2007 - 07:31 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
If you tell the computer to clear the screen it will clear the screen. Thats how it works. You could write your own routine that would clear only a region of the screen but it would be very platform dependent (so I would need to know more about your platform).

I think you are using conio.h, so you should be able to use gotoxy() to position the cursor just below the line (=========) and then print a lot of spaces, which would clear that input away.

Maybe just a better design would help. Your code already contains the goto statement which is generally not considered a good programming practice. You should try to use the predefined loops available to you (such as a do-while loop which would do the same thing as your goto statement).


User is offlineProfile CardPM
+Quote Post

imamkomc
RE: Hold Display Specific Area After Clrsrc() ?
3 Nov, 2007 - 05:56 PM
Post #3

D.I.C Head
Group Icon

Joined: 9 May, 2007
Posts: 62


Dream Kudos: 225
My Contributions
I am utilise platform windows XP sp2.
I would be change my code using do-while loop:

CODE

.
void x::y()
{
int n;
      do{
      cout<<"input number:";  
      cin>>n;
           if (n%2==0){
              cout<<"<warning>, input number must odd\n";
              sleep(2);
              clrscr();
              cout<<"\ntest number :";
              cout<<"\n===================\n";
           }
      }while(n%2==0);
      cout<<"your input odd number is :"<<n;
}
.


Please give me comment.
Thanks advance.

Imamkomc.

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hold Display Specific Area After Clrsrc() ?
3 Nov, 2007 - 07:07 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
ok... start with playing around with this snippet, it has lots of functions that we will want to use. To this collection of functions we want to add a function called ClearConsoleFrom(int row) that will clear the console from a specified row on down.

The function would look like:
CODE
void ClearConsoleFrom(int row)
{
    //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, row};
     //A return value... indicating how many chars were written
     //   not used but we need to capture this since it will be
     //   written anyway (passing NULL causes an access violation).
     DWORD count;
     //This is a structure containing all of the console info
     // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     //Here we will set the current color
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * (csbi.dwSize.Y - row), coord, &count);
          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * (csbi.dwSize.Y - row), coord, &count );
          //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}



So using your initial program as a base this is what I came up with:
CODE
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;


void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole();
void ClearConsoleFrom(int row);
void ClearConsole(int ForgC, int BackC);
void SetColor(int ForgC, int BackC);
void SetColor(int ForgC);
void HideCursor();
void ShowCursor();

class x {
     public :
     void y();
};

void x::y()
{
    int n;
start:
    gotoXY(0,4);
    cout<<"input number:";  
    cin>>n;
    if (n%2==0){
    cout<<"<warning>, input number must odd\n";
    Sleep(2000);
    ClearConsoleFrom(4);
    goto start;
    }

}

void main (){
    x run;
    ClearConsole(15, 1);
    cout<<"\ntest number :\n";
    cout<<"\n===================\n";
    run.y();
    cin.get();
}




//This will clear the console while setting the forground and
//  background colors.
void ClearConsole(int ForgC, int BackC)
{
     WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
     //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, 0};
     //A return value... indicating how many chars were written
     //   not used but we need to capture this since it will be
     //   written anyway (passing NULL causes an access violation).
     DWORD count;

     //This is a structure containing all of the console info
     // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     //Here we will set the current color
     SetConsoleTextAttribute(hStdOut, wColor);
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
          
          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
          //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}

//This will clear the console.
void ClearConsole()
{
     //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, 0};
     //A return value... indicating how many chars were written
     //   not used but we need to capture this since it will be
     //   written anyway (passing NULL causes an access violation).
     DWORD count;
     //This is a structure containing all of the console info
     // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     //Here we will set the current color
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
          //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}

void ClearConsoleFrom(int row)
{
    //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, row};
     //A return value... indicating how many chars were written
     //   not used but we need to capture this since it will be
     //   written anyway (passing NULL causes an access violation).
     DWORD count;
     //This is a structure containing all of the console info
     // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     //Here we will set the current color
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * (csbi.dwSize.Y - row), coord, &count);
          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
          //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}


//This will set the position of the cursor
void gotoXY(int x, int y)
{
     //Initialize the coordinates
     COORD coord = {x, y};
     //Set the position
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
     return;
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
     WORD wColor;
     //We will need this handle to get the current background attribute
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi;
    
     //We use csbi for the wAttributes word.
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
        //Mask out all but the background attribute, and add in the forgournd color
          wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
          SetConsoleTextAttribute(hStdOut, wColor);    
     }
     return;
}

//This will set the forground and background color for printing in a console window.
void SetColor(int ForgC, int BackC)
{
     WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);    
     return;
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
    DWORD count;
     WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
     return;
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
    DWORD count;
     COORD coord = {x, y};
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hStdOut, coord);
     WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
     return;
}

//Hides the console cursor
void HideCursor()
{
     CONSOLE_CURSOR_INFO cciCursor;
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
     if(GetConsoleCursorInfo(hStdOut, &cciCursor))
     {
          cciCursor.bVisible=FALSE;
     }
     return;
}

//Shows the console cursor
void ShowCursor()
{
     CONSOLE_CURSOR_INFO cciCursor;
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
     if(GetConsoleCursorInfo(hStdOut, &cciCursor))
     {
          cciCursor.bVisible=TRUE;
     }
     return;
}

User is offlineProfile CardPM
+Quote Post

The Midnighter
RE: Hold Display Specific Area After Clrsrc() ?
22 Nov, 2007 - 10:51 AM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 5


My Contributions
That's very handy, thank you for that post. =)

I'm wondering if ti's possible to make it clear from LINE to LINE.. I've been messing around with it, but I've never done any console modding like this before.
=(
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hold Display Specific Area After Clrsrc() ?
22 Nov, 2007 - 11:47 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
sure it is... I have not tested it but this would be the basic logic:
CODE
void ClearRows(int row1, int row2)
{
    //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, row};
     //A return value... indicating how many chars were written
     //   not used but we need to capture this since it will be
     //   written anyway (passing NULL causes an access violation).
     DWORD count;
     //This is a structure containing all of the console info
     // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     //Here we will set the current color
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * (row2-row1), coord, &count);
          //This sets the forground/background color
          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * (row2-row1), coord, &count );
          //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}


The FillConsoleOutputCharacter() is the key...

FillConsoleOutputCharacter(handle, char, size, coord, &count);

the handle is the handle for the current console
char is the character that you would like to write.
size is the number of times you want to write the character.
coord is where to start.
&count returns the number of characters actually written.

So to clear just a single row we would set coord = {0, row} and then set the size to width of 1 row (csbi.dwSize.X).
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month