Color in programme

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

User avatar
arnob
Posts: 8
Joined: Fri Feb 24, 2012 3:19 pm

Color in programme

Unread post by arnob » Tue Mar 13, 2012 9:13 pm

I am unable to use cprintf (color print) in codeblocks . I have used :
textcolor(GREEN);
cprintf("anything");
but compiler is showing problem.Can anyone please help :cry:

Hasib
Posts: 238
Joined: Fri Dec 10, 2010 11:29 am
Location: খুলনা, বাংলাদেশ
Contact:

Re: Color in programme

Unread post by Hasib » Mon Mar 26, 2012 11:26 am

Show the full code please...
Last edited by *Mahi* on Mon Mar 26, 2012 11:42 am, edited 1 time in total.
Reason: Corrected capitalization
A man is not finished when he's defeated, he's finished when he quits.

User avatar
arnob
Posts: 8
Joined: Fri Feb 24, 2012 3:19 pm

Re: Color in programme

Unread post by arnob » Mon Mar 26, 2012 2:34 pm

#include<stdio.h>
int main()
{
textcolor(GREEN);
cprintf("anything");
getch();
}

User avatar
*Mahi*
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974
Contact:

Re: Color in programme

Unread post by *Mahi* » Mon Mar 26, 2012 3:03 pm

In dos environment, your code is right. But in a windows machine, you have to handle things differently. This is an example-

Code: Select all

#include <stdio.h>
#include <windows.h>

int main ( void )
{
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  WORD wOldColorAttrs;
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

  /*
   * First save the current color information
   */
  GetConsoleScreenBufferInfo(h, &csbiInfo);
  wOldColorAttrs = csbiInfo.wAttributes;

  /*
   * Set the new color information
   */
  SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );

  printf ( "This is a test\n" );

  /*
   * Restore the original colors
   */
  SetConsoleTextAttribute ( h, wOldColorAttrs);
  return 0;
}
Please read Forum Guide and Rules before you post.

Use $L^AT_EX$, It makes our work a lot easier!

Nur Muhammad Shafiullah | Mahi

User avatar
arnob
Posts: 8
Joined: Fri Feb 24, 2012 3:19 pm

Re: Color in programme

Unread post by arnob » Mon Mar 26, 2012 8:39 pm

Thank you.

Post Reply