Pelles C forum

C language => User contributions => Topic started by: dxl on September 17, 2005, 01:48:20 pm

Title: print coloured messages in console
Post by: dxl on September 17, 2005, 01:48:20 pm
example of printing coloured text in console box:



#include <windows.h>

void my_print(char* string,WORD color)
{
  HANDLE hout;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  COORD point;
  int nsize;
  DWORD output;
  point.X=0;
  hout=GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleScreenBufferInfo(hout,&csbi);
  point.Y=csbi.dwSize.Y-2; /* or point.Y=23 */
  nsize=lstrlen(string);
  WriteFile(hout,string,nsize,&output,0);
  FillConsoleOutputAttribute(hout,color,nsize,point,&output);
}  

void main(void)
{
my_print("DO YOU LIKE GREEN?\x0a",FOREGROUND_GREEN);
my_print("DO YOU LIKE BLUE?\x0a",FOREGROUND_BLUE);
}



makefile:

POC_PROJECT_PATH = .
CC = pocc.exe
LINK = polink.exe

POC_INCLUDE =
CCFLAGS = -c -Ze -Zl -Tx86-coff -Ox -W1 -Gd -Ic:\progra~1\pellesc\include\ -Ic:\progra~1\pellesc\include\win
LINKFLAGS = /entry:main /nodefaultlib /merge:.data=.text /merge:.rdata=.text /machine:ix86 /subsystem:console kernel32.lib

"$(POC_PROJECT_PATH)\myprint.EXE": \
  "$(POC_PROJECT_PATH)\myprint.OBJ"  
  $(LINK) $(LINKFLAGS) -out:"$@" $**

"$(POC_PROJECT_PATH)\myprint.OBJ": \
  "$(POC_PROJECT_PATH)\myprint.c"  
  $(CC) $(CCFLAGS) "$!" -Fo"$@"

result:  myprint.exe 1024 bytes :)
Title: print coloured messages in console
Post by: dxl on September 17, 2005, 05:48:50 pm
previous code seems not working fine...
lets try:

#include <windows.h>
#define FOREGROUND_NORMAL FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
void cprintln(char* string,WORD color)
{
  HANDLE hout;
  DWORD output;
  hout=GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hout,color);
  WriteFile(hout,string,lstrlen(string),&output,0);
  SetConsoleTextAttribute(hout,FOREGROUND_NORMAL);
  WriteFile(hout,"\x0a\x0d",2,&output,0);
}  

void main(void)
{
cprintln("DO YOU LIKE GREEN?",FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cprintln("DO YOU LIKE BLUE?",FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
Title: print coloured messages in console
Post by: Pelle on September 17, 2005, 06:28:13 pm
From v4.0 the following may also be used - larger executable because of "printf" support, but less source code:

Code: [Select]

#define _CONIO_RETRO_COLORS
#include <conio.h>

int main(void)
{
    _textcolor(LIGHTGREEN); cprintf("DO YOU LIKE GREEN?\n");
    _textcolor(LIGHTBLUE); cprintf("DO YOU LIKE BLUE?\n");
    _textcolor(LIGHTGRAY);
    return 0;
}


Pelle
Title: print coloured messages in console
Post by: dxl on September 17, 2005, 07:03:04 pm
wich library is _textcolor from?
Title: print coloured messages in console
Post by: Pelle on September 17, 2005, 08:36:25 pm
Standard C runtime (crt.lib, crtmt.lib, pocrt.dll/pocrt.lib) - but as I said, you need version 4.0.

The most recent beta is here:
http://smorgasbordet.com/phpBB2/viewtopic.php?t=758

Pelle
Title: print coloured messages in console
Post by: dxl on September 17, 2005, 08:44:40 pm
the last code i have posted and the pellesc code posted dont work perfectly in some case:

i run norton commander 5.0 (a msdos program) ,i use it as file manager
when i run both code the prompt is blue!!! (under windows 2k)
i think under win98se, launching it from norton commander is ok

if i run the code in a simple console its ok (both win2k / win98se)
Title: print coloured messages in console
Post by: andre104 on November 11, 2005, 03:39:40 am
this is a console project, right ?
I'm using Pelles C v.400.50
After I copy & paste & compile, I get this error message :
POLINK: error: Unresolved external symbol '_cprintf

What's wrong ?
Title: print coloured messages in console
Post by: Pelle on November 11, 2005, 07:19:59 pm
Check "Define compatibility names" (use /Go compiler switch) or change cprintf to _cprintf.

Pelle
Title: print coloured messages in console
Post by: ber on June 10, 2006, 01:39:32 pm
Quote from: "Pelle"
From v4.0 the following may also be used - larger executable because of "printf" support, but less source code:

Code: [Select]

#define _CONIO_RETRO_COLORS
#include <conio.h>

int main(void)
{
    _textcolor(LIGHTGREEN); cprintf("DO YOU LIKE GREEN?\n");
    _textcolor(LIGHTBLUE); cprintf("DO YOU LIKE BLUE?\n");
    _textcolor(LIGHTGRAY);
    return 0;
}


Pelle