i have code for write on console and change the text color and backcolor and clear screen(and much more) inside of a nice class console.
these function change the text color and back color:

    void SetColorAndBackground(int ForgC, int BackC=0)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|(BackC<<4) );
    }

but is there any way for blink the text?

Recommended Answers

Use a loop to constantly change the backcolor to the forecolor and back again. You might need an extra delay loop in there as well.

Jump to Post

Doesn't blink, using VC++ 2012. This version blinks. I marked the lines I changed with //<<<<<<<<<<<<<<

//blinktext.h
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>
#include <iostream>

using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink …
Jump to Post

All 12 Replies

Use a loop to constantly change the backcolor to the forecolor and back again. You might need an extra delay loop in there as well.

i'm trying do that and i must put that loop on a thread..

#include <iostream>
 #include <windows.h>

 using namespace std;

 void gotoxy (int x, int y);

 int main()
 {
     while (true)
     {
         gotoxy(0,0); // move to where we want to output
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
         string Text="hello world";
         char *EmptyText;
         EmptyText=new char[ Text.length() ] ;
         int i;
         for( i=0;i<=Text.length();i++)
            {
                EmptyText[i] = ' ';
            }
            EmptyText[i] ='\0';
         cout << Text; // overwrite the current output
         Sleep(1000);
            gotoxy(0,0); // move back to the start of output
         cout << EmptyText; // this will reset the output to blank
            Sleep(500);
    }

     cin.get();
     return EXIT_SUCCESS;
 }

 void gotoxy (int x, int y)
 {
     COORD coord; // coordinates
     coord.X = x; coord.Y = y; // X and Y coordinates
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
 }
  • these type of loop don't consumes very CPU?
  • i'm confused with 1 thing: the delay can give problems for get\set the caret position?(you can read something and the caret can moves... if you understand what i mean)
    unless i use api functiions for print the blink text;)
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>

using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

     string Text="hello world";
    char *EmptyText;
    EmptyText=new char[ p->Text.length() ] ;
    int i;
    for( i=0;i<=(int)Text.length();i++)
    {
        EmptyText[i] = ' ';
    }
    EmptyText[i] ='\0';

    HDC hDC=GetDC(GetConsoleWindow());
    SetTextColor(hDC,RGB(0,255,0));
    SetBkMode(hDC,TRANSPARENT);

    while (true)
    {
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length()+1);
        Sleep(1000);
        TextOut(hDC,p->x,p->y,EmptyText,(int)p->Text.length()+1);
        Sleep(500);
    }
    return 0;
}
void TextBlink(string Text, int x, int y, int TextColor, int BackColor)
{
    Blink *b;
    b->Text=Text;
    b->BackColor=BackColor;
    b->TextColor=TextColor;
    b->x=x;
    b->y =y;

    _beginthreadex(NULL, 0, BlinkLoop  ,&b, 0, NULL);
}

finally i have the code, but i belive the error is from pointers... because the windows tell me that the program stops to working and then close it.
can anyone advice me?

line 23: failed to allocate memory for the null string terminator.

Lines 37 and 39: remove +1 from Text.Length().

finally i have the blink working:

//blinktext.h
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>

using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    size_t tlen = p->Text.length()-2;
    char *EmptyText = new char[tlen];
    memset(EmptyText, 'd', tlen);

    HDC hDC=GetDC(GetConsoleWindow());

    while (true)
    {

        SetBkMode(hDC,TRANSPARENT);
        SetTextColor(hDC,RGB(0,255,0));
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length()+1);
        Sleep(500);
        SetTextColor(hDC,RGB(0,0,0));
        SetBkMode(hDC,OPAQUE);
        SetBkColor(hDC,RGB(0,0,0));
        TextOut(hDC,p->x,p->y,EmptyText,(int)p->Text.length()+2);
        Sleep(500);
    }
    return 0;
}

void TextBlink(string text, int x, int y, int TextColor, int BackColor)
{
    Blink *b=new Blink;
    b->Text=text;
    b->BackColor=BackColor;
    b->TextColor=TextColor;
    b->x=x;
    b->y =y;
    _beginthreadex(NULL, 0, BlinkLoop  ,b, 0, NULL);
}

//main.cpp
#include <iostream>
#include "blinktext.h"


using namespace std;

int main()
{
    TextBlink("Hello world", 20,20,3,3);
    cout << "hello world";//like you see, the caret(text cursor) isn't losed


    cin.get();
    return 0;
}

why (int)p->Text.length()+2)??
because some issue..i don't know, if i don't use it the text isn't showed... sorry, but test it and see yourself...
the colors aren't programmed, but works fine

commented: This doesn't blink +0

Doesn't blink, using VC++ 2012. This version blinks. I marked the lines I changed with //<<<<<<<<<<<<<<

//blinktext.h
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>
#include <iostream>

using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    size_t tlen = p->Text.length()+1; //<<<<<<<<<<<<<<
    char *EmptyText = new char[tlen];
    memset(EmptyText, 'd', tlen);

    HDC hDC=GetDC(GetConsoleWindow());

    while (true)
    {

        SetBkMode(hDC,TRANSPARENT);
        SetTextColor(hDC,RGB(0,255,0));
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length());//<<<<<<<<<<<<<<
        Sleep(500);
        SetTextColor(hDC,RGB(0,0,0));
        SetBkMode(hDC,OPAQUE);
        SetBkColor(hDC,RGB(0,0,0));
        TextOut(hDC,p->x,p->y,EmptyText,(int)p->Text.length());//<<<<<<<<<<<<<<
        Sleep(500);
    }
    return 0;
}

void TextBlink(string text, int x, int y, int TextColor, int BackColor)
{
    Blink *b=new Blink;
    b->Text=text;
    b->BackColor=BackColor;
    b->TextColor=TextColor;
    b->x=x;
    b->y =y;
    _beginthreadex(NULL, 0, BlinkLoop  ,b, 0, NULL);
}



int main()
{
    TextBlink("Hello world", 20,20,3,3);
    cout << "hello world";//like you see, the caret(text cursor) isn't losed


    cin.get();
    return 0;
}
commented: Yup this works as well +2

sorry about that. or it's compiler mingw bbug, i don't know...
see my new code with colors working:

//blinktext.h

#define WINVER 0x0501
#define _WIN32_WINNT WINVER
#include <windows.h>
#include <process.h>

#include <string>
#include <iostream>
using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    COLORREF TextColor;
    COLORREF BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    int tlen = (int)p->Text.length();

    HDC hDC = GetDC(GetConsoleWindow());

    HBRUSH hb = CreateSolidBrush(p->BackColor);

    LOGFONT lf;

    GetObject(GetStockObject(OEM_FIXED_FONT), sizeof(LOGFONT), &lf);
    lf.lfWeight = FW_REGULAR;

    HFONT hf = CreateFont(lf.lfHeight, lf.lfWidth,
            lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
            lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
            lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
            lf.lfPitchAndFamily, lf.lfFaceName);

    SelectObject(hDC, (HGDIOBJ)(HFONT)hf);
    SetTextColor(hDC, p->TextColor);
    SetBkMode(hDC, TRANSPARENT);

    TEXTMETRIC tm;

    GetTextMetrics(hDC, &tm);

    RECT rt;

    rt.left = p->x * tm.tmAveCharWidth;
    rt.top = p->y * tm.tmHeight;
    rt.right = rt.left + tlen * tm.tmAveCharWidth;
    rt.bottom = rt.top + tm.tmHeight;

    while (true)
    {
        TextOut(hDC, rt.left, rt.top, p->Text.c_str(), tlen+1);//readers if the text isn't showed take of the '+1'
                Sleep(500);
        FillRect(hDC, &rt, hb);
                Sleep(500);
    }
    return 0;
}

void TextBlink(const string& Text, int x, int y, COLORREF TextColor, COLORREF BackColor)
{
    Blink *b = new Blink;
    b->Text = Text;
    b->BackColor = BackColor;
    b->TextColor = TextColor;
    b->x=x;
    b->y =y;

    _beginthreadex(NULL, 0, BlinkLoop, b, 0, NULL);
}

//main.cpp

#include <iostream>
#include "blinktext.h"


using namespace std;

int main()
{
    TextBlink("This text blinks ok", 0, 0, RGB(0, 255, 0), RGB(0, 0, 0));
    TextBlink("and so does this text", 4, 5, RGB(255, 255, 255), RGB(0, 0, 0));
    TextBlink("this text also blinks ok", 6, 8, RGB(255, 255, 255), RGB(0, 0, 0));


    cin.get();
    return 0;
}

(readers don't forget add the gdi32 library to the linker options)
i left a comment for the readers for avoid problems in other compilers.
like you see now we can use colors ;)
isn't perfect because of 1 thing:

TextOut(hDC, rt.left, rt.top, "                        ", strlen("                        ")+1);

why don't print all space characters?
(i realy mean spaces, because the others characters works fine)

commented: This does blink +2

now i have a new version of the blink text:

//blinktext.h
#include <windows.h>
#include <process.h>

#include <string>
#include <iostream>
using namespace std;

//ForeColor
const int ForeColorBlack        = 0;
const int ForeColorBlue         = FOREGROUND_BLUE;
const int ForeColorGreen        = FOREGROUND_GREEN;
const int ForeColorCyan         = FOREGROUND_BLUE | FOREGROUND_GREEN;
const int ForeColorRed          = FOREGROUND_RED;
const int ForeColorMagenta      = FOREGROUND_BLUE | FOREGROUND_RED;
const int ForeColorBrown        = FOREGROUND_GREEN | FOREGROUND_RED;
const int ForeColorLightGray    = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
const int ForeColorDarkGray     = FOREGROUND_INTENSITY;
const int ForeColorLightBlue    = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const int ForeColorLightGreen   = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int ForeColorLightCyan    = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int ForeColorLightRed     = FOREGROUND_RED |  FOREGROUND_INTENSITY;
const int ForeColorLightMagenta = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
const int ForeColorYellow       = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
const int ForeColorWhite        = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;

//BackColor
const int BackColorBlack        = 0;
const int BackColorBlue         = BACKGROUND_BLUE;
const int BackColorGreen        = BACKGROUND_GREEN;
const int BackColorCyan         = BACKGROUND_BLUE | BACKGROUND_GREEN;
const int BackColorRed          = BACKGROUND_RED;
const int BackColorMagenta      = BACKGROUND_BLUE | BACKGROUND_RED;
const int BackColorBrown        = BACKGROUND_GREEN | BACKGROUND_RED;
const int BackColorLightGray    = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
const int BackColorDarkGray     = BACKGROUND_INTENSITY;
const int BackColorLightBlue    = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
const int BackColorLightGreen   = BACKGROUND_GREEN | BACKGROUND_INTENSITY;
const int BackColorLightCyan    = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
const int BackColorLightRed     = BACKGROUND_RED |  BACKGROUND_INTENSITY;
const int BackColorLightMagenta = BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
const int BackColorYellow       = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
const int BackColorWhite        = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;

struct Blink
{
    string Text;
    SHORT x;
    SHORT y;
    WORD  TextColor;
    WORD  BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);
    SHORT tlen = static_cast<SHORT>(p->Text.length());
    SHORT x=p->x, y=p->y;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    const char *text=(char*)p->Text.c_str();
    CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
    CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
    COORD a={tlen,1}, b={0,0};
    SMALL_RECT c={x, y,SHORT(x+tlen),SHORT(y+1)};
    int i=0;
    for (i=0; i<=tlen;i++)
    {
        ConsoleText[i].Char.AsciiChar  =text[i];
        ConsoleText[i].Attributes=p->TextColor|p->BackColor;
        EmptyConsoleText[i].Char.AsciiChar  =' ';
        EmptyConsoleText[i].Attributes=p->TextColor|p->BackColor;
    }

    while (true)
    {
        WriteConsoleOutput(hout,ConsoleText,a,b,&c);
        Sleep(500);
        WriteConsoleOutput(hout,EmptyConsoleText,a,b,&c);
        Sleep(500);
    }
    return 0;
}

void TextBlink(const string& Text, SHORT  x, SHORT  y, const WORD TextColorAttribute=ForeColorWhite, const WORD BackColorAttribute=BackColorBlack)
{
    Blink *b = new Blink;
    b->Text = Text;
    b->BackColor = BackColorAttribute;
    b->TextColor = TextColorAttribute;
    b->x=x;
    b->y =y;

    _beginthreadex(NULL, 0, BlinkLoop, b, 0, NULL);
}


//main.cpp
#include <iostream>
#include "blinktext.h"
#include <string>

using namespace std;

int main()
{
    TextBlink("This text blinks ok", 0, 2);
    TextBlink("This text blinks ok", 0, 4,ForeColorBlue);
    TextBlink("This text blinks ok", 0, 6);
    string name="";
    getline(cin,name);
    cout << name;
    cin.get();

    return 0;
}

it's more cool, but less colors.. here we just use the console colors(they are 16).
thanks for all

thanks for the comment..thank you.
sometimes my brain come with something ;)
i accept sugestions for timer loop and feedbacks;)