DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Programmatically effecting console font colors and size in Delphi console app  (Read 25,185 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
I've Googled this quite a bit and just can't find what I'm looking for; I'm seeking a way to programmatically alter the font color and size in a Delphi console mode application.  I've changed these settings manually on my computer by right-clicking the titlebar of a running Delphi console app, then changing these from the context menu that pops up.  In .NET, it's pretty easy to alter foreground color, background color, etc.  But I'm just not finding any similar code snippets for Delphi.  Can anyone help?
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
no idea, sorry.

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
For colors, try this:


Code: (delphi)
// This will cause text to be red:

  SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_RED or
                          FOREGROUND_INTENSITY);

// This sets it to white:

  SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_RED or
                          FOREGROUND_GREEN or
                          FOREGROUND_BLUE);

Example:

http://coding.derkeiler.com/Archive/Delphi/alt.comp.lang.borland-delphi/2003-12/0303.html

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Thanks, app, that worked well.  Now if I can just find something to programmatically effect the console font size, I'm in business.  Looks like WinAPI is the place to search...
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
You could change the font used in the whole console window with SetCurrentConsoleFontEx, but it only works on Vista, Server 2008, and Win7.

I haven't found anything about changing text size, unfortunately.

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
Have you tried following the .net code back to its source? You might find your answers there.

Sometimes the managed way of doing it is similar enough you can translate it back to unmanaged.
« Last Edit: January 18, 2010, 04:25 AM by app103 »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
That's what I'm in the process of doing now.  I'll let you know if I find a solution.  Thanks!
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
I was just told the following by a friend:

Quote
For Vista and above, there is SetCurrentConsoleFontEx, as already has been said.
For 2K and XP, there is an undocumented function SetConsoleFont; e.g. read here.
here is some code if your fellow can read/translate C
http://stackoverflow.com/questions/1922294/using-unicode-font-in-c-console-app
'HKEY_CURRENT_USER\Console' <--- registry path to alter for saving settings.
"console window how to change font" <-- my google search.
« Last Edit: January 18, 2010, 04:47 AM by app103 »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Thanks, App!  I will try this.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
First: please don't even consider changing the default console appearance in the registry in order to achieve something.

Second: can't you just create a shortcut to your .exe and specify the font?
- carpe noctem

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
I've got a good portion of my original goal down: (1) be able to change font and color programmatically.  The only thing I haven't been able to figure out how to do is to change the font size programmatically.  It just doesn't seem that this should be so obscure in a language as well-liked and widely used as Delphi.

No, I wouldn't even consider touching the Registry. 
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
I guess the shortcut route might work, but then the end-user would have to use that particular shortcut to start the program, in order to get the desired font, right?
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Shortcuts don't change the
font of an existing console. They provide the initial console settings. And
they don't do it by calling any API. The console subsystem parse shortcut
files directly. By the way, when we have a shortcut and set the font
property, a registry key with the same name of the shortcut file will be
added to HKCU\Console, similar to the second method.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Hm, I'm not actually sure whether the shortcut would have to be used to start the application - haven't checked. Back in the days for DOS apps, the settings were iirc read&applied even though you didn't launch the app through the shortcut - dunno if this applies to win32 console mode apps, they are quite different from DOS executables :)

By the way, when we have a shortcut and set the font property, a registry key with the same name of the shortcut file will be added to HKCU\Console, similar to the second method.
Didn't know that - but I'd consider it an implementation detail, unless it's clearly documented in some official MS doc.

It just doesn't seem that this should be so obscure in a language as well-liked and widely used as Delphi.
It's been quite some years since I used Delphi, but my impression from back then was that the VCL handles standard stuff - if you need something slightly more obscure, especially if it's not super-officially documented, you have to go API?
- carpe noctem

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Yep, that's the impression I'm forming as well.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Original address: https://www.donationcoder.com/forum/index.php?topic=21398.0