Software & Finance





Visual C++ - Scroll Text using SetConsoleCursorPosition and printf functions





Here is the Turbo C Source Code Scroll Text using gotoxy and printf functions

 

The function Scroll will accept a string as input and it will display the scrolling text on the top on the top of the screen Row position 1.

 


Source Code


 

#include <stdlib.h>

#include <string.h>

#include <dos.h>

#include <conio.h>

#include <tchar.h>

#include <windows.h>

 

 

void gotoxy(int x, int y)

{

    COORD ord;

    ord.X = x;

    ord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

}

 

void scroll(const char *str)

{

    int pos = 0;

    int len = strlen(str);

    char buf[128];

    while(!kbhit())

    {

        gotoxy(pos,0);

        printf("%s", str);

        Sleep(50);

        gotoxy(pos,0);

        if(pos > 80 - len)

        {

            sprintf(buf, "%%%ds", len);

            printf(buf, " ");

            pos = 0;

        }

        else

        {

            printf(" ");

        }

        pos++;

    }

}

 

int main(int argc, TCHAR *argv[])

{

    char message[64];

    int len = 0;

    if(argc < 2)

    {

        printf("Usage: scroll.exe <text>");

        exit(0);

    }

 

    strcpy(message, (const char*) argv[1]);

    len = strlen(message);

 

    scroll(message);

 

    return 0;

}

Output


C:\VCPP\Samples> Scroll.exe "Welcome to softwareandfinance.com"

 

Scrolling text of welcome to softwareandfinance.com