NO

Author Topic: print coloured messages in console  (Read 9751 times)

dxl

  • Guest
print coloured messages in console
« 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 :)

dxl

  • Guest
print coloured messages in console
« Reply #1 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);
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
print coloured messages in console
« Reply #2 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
/Pelle

dxl

  • Guest
print coloured messages in console
« Reply #3 on: September 17, 2005, 07:03:04 PM »
wich library is _textcolor from?

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
print coloured messages in console
« Reply #4 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
/Pelle

dxl

  • Guest
print coloured messages in console
« Reply #5 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)

andre104

  • Guest
print coloured messages in console
« Reply #6 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 ?

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
print coloured messages in console
« Reply #7 on: November 11, 2005, 07:19:59 PM »
Check "Define compatibility names" (use /Go compiler switch) or change cprintf to _cprintf.

Pelle
/Pelle

ber

  • Guest
print coloured messages in console
« Reply #8 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