* * *

Author Topic: How to find out text / background colors in console app?  (Read 1763 times)

glorfin

  • New member
  • *
  • Posts: 30
How to find out text / background colors in console app?
« on: February 04, 2018, 05:15:45 pm »
Hi!
I am writing a console program using CRT unit. Is there a cross-platform way to find out text and background colors of a console?
And another question: at least in windows 10, WindMaxY reports not physical size of the window, which in my case is 25 lines but, apparently, console buffer size (300 lines). Is there a way to find the window height?

Thank you in advance!

jamie

  • Hero Member
  • *****
  • Posts: 835
Re: How to find out text / background colors in console app?
« Reply #1 on: February 04, 2018, 09:23:43 pm »
There is a TConsoleBuf Type which is an array of the screen.

Then there is the PconsoleBuf a type of TConsoleBuf which is an array of


TCharAttr

Declaration

Source position: crt.pp line 29

type TCharAttr = packed record
  ch: Char;        Character code
  attr: Byte;        Attributes
end;

Documentation generated on: Feb 06 2017

glorfin

  • New member
  • *
  • Posts: 30
Re: How to find out text / background colors in console app?
« Reply #2 on: February 05, 2018, 11:42:12 pm »
Thank you, Jamie!
But looks like this is true only for CRT for UNIX. Looks like cross-platform way does not exist. OK, I'll use this way for Unix and GetConsoleScreenBufferInfo for Windows.
There is a TConsoleBuf Type which is an array of the screen.

Then there is the PconsoleBuf a type of TConsoleBuf which is an array of


TCharAttr

Declaration

Source position: crt.pp line 29

type TCharAttr = packed record
  ch: Char;        Character code
  attr: Byte;        Attributes
end;

Documentation generated on: Feb 06 2017

jamie

  • Hero Member
  • *****
  • Posts: 835
Re: How to find out text / background colors in console app?
« Reply #3 on: February 07, 2018, 01:16:18 am »
for windows its

 "ReadConsoleOutPutAttribute"

  (OutputHandle, Abuffer to receive_atts, number_Of_Atts_To_receive,Coordinates of First Cell, Number of cells Read);

 Not sure why this isn't in the CRT already ?

  :-[

glorfin

  • New member
  • *
  • Posts: 30
Re: How to find out text / background colors in console app?
« Reply #4 on: May 07, 2018, 12:20:03 am »
Jamie, thank you for your reply!
Sorry that so late, but it's better than never :)

Solution which worked for me on windows is finally:


Code: Pascal  [Select]
  1. uses Windows;
  2. var ConsoleInfo:TConsoleScreenBufferInfo;
  3.  
  4. function GetConsoleTextColor:byte;
  5. begin
  6.   if GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), ConsoleInfo) then
  7.     Result := Lo(ConsoleInfo.wAttributes)
  8.   else
  9.     Result := LightGray; //just default value if function does not work
  10. end;
  11.  

Hi(ConsoleInfo.wAttributes) gives background color.

And since it is not in CRT, I created unit CRTExt for myself ;)

Thanks once more!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus