Skip to content

Instantly share code, notes, and snippets.

@Codeplaza
Created November 5, 2013 10:25
Show Gist options
  • Save Codeplaza/7316954 to your computer and use it in GitHub Desktop.
Save Codeplaza/7316954 to your computer and use it in GitHub Desktop.
#include <windows.h> //header file for windows
#include <stdio.h> //C standard library
void SetColor(int ForgC);
int main()
{
SetColor(34);
printf("test colour");
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 foreground color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment