Software & Finance





Visual C++ Console Application - Blinking Text using SetConsoleCursorPosition





You might be thinking that whether is it possible to display text at a specified location (row, col in console application where no graphics are present? Yes, It is possible to display text at a particular location. All you need to do is put the custor position using SetConsoleCursorPosition function.

 

To get the blinking text on the screen is very simple. Just do a cout of a string at a particular position and then display the blank string at the same location. Use Sleep function to control the blinking speed.

 

SetTextXY expects the number of times to blink also. If you expext this to happen indefinitely and also with out using blocking the execution, the either you need multi threading or WM_TIMER event. I already gave samples on multi threading and WM_TIMER, I did not write that part of code here. SetTextXY function just takes row, col and string to display along with number of times to blink and speed.

 


Source Code


#include <tchar.h>

#include <conio.h>

#include <process.h>

#include <dos.h>

#include <iostream>

#include <windows.h>

 

using namespace std;

 

void SetTextXY(int x, int y, char *buf, int timestoBlink, int delayMilliSecs)

{

    ::system("cls");

    COORD ord;

    ord.X = x;

    ord.Y = y;

 

    int len = strlen(buf);

    char *p = new char[len + 1];

    memset(p, 32, len);

    p[len] = '\0';

 

    for(int i = 0; i < timestoBlink; i++)

    {

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

        std::cout << p;

        ::Sleep(300);

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

        std::cout << buf;

        ::Sleep(delayMilliSecs);

       

    }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

    char buf[] = "Please visit us at: http://www.softwareandfinance.com \n\n\n";

 

    SetTextXY(15, 1, buf, 5, 1000);

 

    return 0;

}

Output


Blinking text of  
Please visit us at: http://www.softwareandfinance.com