Pages

Saturday, 23 April 2016

Changing Text Color

If you want to change the color of text in console windows without using graphic then you can use this code in codeblock.


#include <windows.h> // Header file for windows
#include <stdio.h>      // C standard library
void SetColor(int ForgC);
int main(){
     SetColor(34);
     printf("test clour");
     return 0;
}
void SetColor(int ForgC){
     WORD wColor; // We will need this handle to get the current background attribute
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi; // We use csbi for the wAttributes word.
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))// Mask out all but the background attribute, and add in the forgournd color
          wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
          SetConsoleTextAttribute(hStdOut, wColor);
     }
     return;
}

No comments:

Post a Comment