Thread: simple question

  1. #1
    moullas
    Guest

    simple question

    i want to make a function that whenever you type exit or quit to display in the center of the screen the name of the guy who did it
    but with a changed background color and another text color besides the normal black and white dos window

    using visual c++ v6 by microsoft

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >i want to make a function that whenever you type exit or quit to >display in the center of the screen the name of the guy who did >it

    The guy who did it? You mean the guy that typed exit or quit somewhere in some window? Please give some more information.

  3. #3
    moullas
    Guest
    sorry i will be more specific

    lets say i make a program and when the program exits i want my name displayed in the middle of the screen with red colors and the background to be yellow for example

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    should work

    I know I have posted this before but this is a small utility to work with to change the colors and change the location of text and what not. Works well with vc++.

    Code:
    #ifndef _CONSOLE_LIBRARY_INTERFACE
    #define _CONSOLE_LIBRARY_INTERFACE
    
    #include <windows.h>
    WORD cl_fc_param = 0;
    WORD cl_bc_param = 0;
    
    void text_color(bool red, bool green, bool blue, bool bright)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    
    	cl_fc_param = 0;
    	if ( red ) cl_fc_param |= FOREGROUND_RED;
    	if ( green ) cl_fc_param |= FOREGROUND_GREEN;
    	if ( blue ) cl_fc_param |= FOREGROUND_BLUE;
    	if ( bright ) cl_fc_param |= FOREGROUND_INTENSITY;
    
    	SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
    }
    void text_background(bool red, bool green, bool blue, bool bright)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    
    	cl_bc_param = 0;
    	if ( red ) cl_bc_param |= BACKGROUND_RED;
    	if ( green ) cl_bc_param |= BACKGROUND_GREEN;
    	if ( blue ) cl_bc_param |= BACKGROUND_BLUE;
    	if ( bright ) cl_bc_param |= BACKGROUND_INTENSITY;
    
    	SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
    }
    void text_loc(int x, int y)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    	COORD cursor_coordinates;
    	cursor_coordinates.X = x;
    	cursor_coordinates.Y = y;
    	SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cursor_coordinates );
    }
    
    void console_title(const char* title)
    {
    	SetConsoleTitle(title);
    }
    
    #endif
    Anyways, here is a snippet of code that should help you accomplish what you want. What it attempts to do is set up a string that it as long as the buffer screen and print it to the string (there probably is a better way, i never tried system("CLS") with colors active). Then it sets the text location to the center minus an offset for the name length and prints it out.

    Code:
        void ShowEndScreen(const char * name)
        {
             const int xmax = 80;
             const int ymax = 25;
    
             int buff[xmax*ymax];
             memset(buff, 0, xmax*ymax); // string.h
    
             text_color(0, 1, 0, 0); // red green blue bright
             text_background(0, 1, 0, 0); // same
             text_loc(0, 0);
    
             cout << buff;
    
    
             text_loc( ymax/2, xmax/2 - strlen(name)/2 ); // I think the row is mentioned first, you might want to flip
             text_color(1, 1, 1, 1); // same as above
    
             cout << name;
        }

  5. #5
    moullas
    Guest
    i appreciate the help man really
    i mean you guys in this forum are amazing


    there is one thing i dont understand though
    shouldnt text_loc,text_color,text_background be initialized or something??

    i mean int text_loc??for example

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    not really

    The default values of the text color and background can be set in the console window. For example, open the command prompt and left click the icon in the top-left corner and select defaults. This will allow you to set the initial text color and background of any console program. As far as the default values for the text location is concerned, I believe that is always zero.

    Normally, the text color is dark white and the background is black. That is what I assume in tha small header file. It is possible to queue the system to find the current color and background, but this would require either a seperate initheader() function or specifying all text color attributes (foreground and background) at once. My solution, if you're really worried about initial colors or such, just call text_color() and text_background() on startup.

    Oh yeah, in that last piece of code, you have to use the column number for the first paramater and row number for the second one. In other words, the dubious call to text_loc above should have its parameters swapped as I mentioned in the comment next to it.

    Oh and I hope you don't mind, I liked your little idea of the end screen option with the screen covered and a small message; so, I went ahead and made a function that did just that. Only mine also shows a little animation of lines coming from the left and right to cover what is currently on the screen before the message is shown.
    Last edited by genghis; 02-04-2002 at 04:42 PM.

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

    problem with this code...

    Genghis:
    i tried to use the function that you posted. however, as soon as my console window scrolls down i lose the ability to change the colors to anything else that the first set that i used. if you have any ideas please let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM