windows - how to change colour of a specific output on console in c++ -


//following codes once change colour, keep them along way, //if want write juxt 1 word different colour cout<<"4june" //should red jxt colour of text should change not background //colour , if display cout<<"colour not changed"; //colour should original one, how attain c++????

#include <iostream> #include <windows.h> #include <winnt.h> #include <stdio.h> #include "stdafx.h" using namespace std;  int main(int argc, char* argv[]) {    handle consolehwnd = getstdhandle(std_output_handle);    cout << "this text not colorized\n";    setconsoletextattribute(consolehwnd, foreground_red);    cout << "this text shows red\n";    setconsoletextattribute(consolehwnd, foreground_blue);    cout << "this text shows blue\n"; }   or  setconsoletextattribute(getstdhandle(std_output_handle),1)  or  system("color 3"); cout<<"colour changed"<<endl;** 

if want red text black background type setconsoletextattribute(getstdhandle(std_output_handle),4); cout<<"4th june";

to reset normal color, set color 7. setconsoletextattribute(getstdhandle(std_output_handle),7);

enter image description here

here table of console colors. can make 256 combinations entering color code number above image desired color.

below console color management code.

#include <windows.h>  #include <iostream> using namespace std;     void gotoxy(int x, int y); void setcolor(word color); void setforegroundandbackgroundcolor(int foregroundcolor,int backgroundcolor); void clrscr();  void printallcolors();    int main() {   // set red text on black background       gotoxy(30,10);   setconsoletextattribute(getstdhandle(std_output_handle),4); cout<<"4th june";      // set white text on black background   gotoxy(1,23);   setconsoletextattribute(getstdhandle(std_output_handle),7);      return 0; }   void setcolor(word color) {     setconsoletextattribute(getstdhandle(std_output_handle),color);     return; }    void setforegroundandbackgroundcolor(int foregroundcolor,int backgroundcolor) {    int color=16*backgroundcolor+foregroundcolor;    setcolor(color); }     void gotoxy(int x, int y) {     coord coord;     coord.x = x; coord.y = y;     setconsolecursorposition(getstdhandle(std_output_handle), coord);     return; }     void clrscr() {     coord coordscreen = { 0, 0 };     dword ccharswritten;     console_screen_buffer_info csbi;     dword dwconsize;     handle hconsole = getstdhandle(std_output_handle);      getconsolescreenbufferinfo(hconsole, &csbi);     dwconsize = csbi.dwsize.x * csbi.dwsize.y;     fillconsoleoutputcharacter(hconsole, text(' '), dwconsize, coordscreen, &ccharswritten);     getconsolescreenbufferinfo(hconsole, &csbi);     fillconsoleoutputattribute(hconsole, csbi.wattributes, dwconsize, coordscreen, &ccharswritten);     setconsolecursorposition(hconsole, coordscreen);     return; }   void printallcolors() {    int ix=0;    int iy=1;    int col=0;    setcolor(7);    clrscr();     // demo setforegroundandbackgroundcolor    (int =0;i<16;i++)    {       for(int j=0;j<16;j++)       {        setforegroundandbackgroundcolor(i,j);        gotoxy(i*5  , iy+j); cout<<""<<i + (16 *j)<<"";        col++;       }    }      setcolor(31);    cout<<"\n";    gotoxy(1,23); }   /*  color      background    foreground --------------------------------------------- black            0           0 blue             1           1 green            2           2 cyan             3           3 red              4           4 magenta          5           5 brown            6           6 white            7           7 gray             -           8 intense blue     -           9 intense green    -           10 intense cyan     -           11 intense red      -           12 intense magenta  -           13 yellow           -           14 intense white    -           15      */ 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -