C++ e C: Movendo e colorindo


Há algum tempo que programo em C/C++, considero ambas as linguagens muito importantes para a formação de um bom programador. Boa parte de meus programas, foi-se necessário a movimentação do cursor e a colorização de caracteres, por este motivo postarei duas das funções mais utilizadas em meus programas, caso alguém se interesse pelo mesmo. É importante destacar a necessidade de inclusão da biblioteca “Windows.h”, utilizando-se de “#include <windows.h>”, para ambos os casos.

A seguir o código para mover o cursor.

void Mover(int linha, int coluna)
{
      HANDLE hOut;
      COORD Posicao;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      Posicao.X = coluna;
      Posicao.Y = linha;
      SetConsoleCursorPosition(hOut, Posicao);
}
A seguir o código para colorir os caracteres.
void MudarCor(int cor)
{
      HANDLE hOut;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hOut, cor);
}
A seguir o código completo para C++.
#include <windows.h>
#include <iostream.h>

void Mover(int linha, int coluna)
{
      HANDLE hOut;
      COORD Posicao;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      Posicao.X = coluna;
      Posicao.Y = linha;
      SetConsoleCursorPosition(hOut, Posicao);
}

void MudarCor(int cor)
{
      HANDLE hOut;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hOut, cor);
}

void main()
{
      Mover(5, 5);
      MudarCor(2);
      cout << "\nHello World!" << endl;
}
A seguir o código completo para C.
#include <windows.h>
#include <stdio.h>

void Mover(int linha, int coluna)
{
      HANDLE hOut;
      COORD Posicao;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      Posicao.X = coluna;
      Posicao.Y = linha;
      SetConsoleCursorPosition(hOut, Posicao);
}

void MudarCor(int cor)
{
      HANDLE hOut;
      hOut = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hOut, cor);
}

void main()
{
      Mover(5, 5);
      MudarCor(2);
      printf("\nHello World!\n");
}
Não tenho certeza das cores pois faz algum tempo que não programo em C/C++, no entanto, pelo que me lembro, algumas das cores disponíveis desta função são identificadas por azul (1), verde (2), azul claro (3), vermelho (4), rosa (5), amarelo (6). Tente testar outros valores para verificar qual a cor desejada.

Liked this article? Then leave a comment, follow this blog and subscribe to our RSS feed. Questions, problems, suggestions or translations? Please contact us. Click here for more instructions to make a small donation.

Be the first to comment

All comments are read and moderated before published.

For every comment
- ALL CAPS or grammatical errors will not be tolerated;
- Do not promote other blogs or websites;
- Do not add unnecessary links in the content of your comment;
- If you want to leave your URL, use the OpenID;
- Promote your e-mail only if really needed;
- Threats, insults or attacks are not allowed;
- Your comment must be related to the subject of the article;

Questions?
- Read all the answers;
- Ask if you can not find the answer;

Comments should contain only the following topics:
- Questions;
- Suggestions;
- Thanks;
- Tips related;

Every comment is a personal opinion of the reader. The authors are not responsible for the content of any comments made by the commenter(s). The authors are also not responsible for knowing whether the content of Your comment is breaking the law in other countries or jurisdictions.

  ©Template by Dicas Blogger and customized by Gugatb.

TOP