Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 06, 2016, 11:58:21 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
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?

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,406
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
no idea, sorry.

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,666
    • View Profile
    • App's Apps
    • Read more about this member.
    • Donate to Member
For colors, try this:


Code: Delphi [Select]
  1. // This will cause text to be red:
  2.  
  3.   SetConsoleTextAttribute(GetStdHandle(
  4.                           STD_OUTPUT_HANDLE),
  5.                           FOREGROUND_RED or
  6.                           FOREGROUND_INTENSITY);
  7.  
  8. // This sets it to white:
  9.  
  10.   SetConsoleTextAttribute(GetStdHandle(
  11.                           STD_OUTPUT_HANDLE),
  12.                           FOREGROUND_RED or
  13.                           FOREGROUND_GREEN or
  14.                           FOREGROUND_BLUE);


Example:

http://coding.derkei...hi/2003-12/0303.html

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
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...

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,666
    • View Profile
    • App's Apps
    • Read more about this member.
    • Donate to Member
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,666
    • View Profile
    • App's Apps
    • Read more about this member.
    • Donate to Member
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 17, 2010, 10:25:50 PM by app103 »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
That's what I'm in the process of doing now.  I'll let you know if I find a solution.  Thanks!

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,666
    • View Profile
    • App's Apps
    • Read more about this member.
    • Donate to Member
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...ont-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 17, 2010, 10:47:42 PM by app103 »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
Thanks, App!  I will try this.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
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,120
    • View Profile
    • Donate to Member
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. 

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
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?

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,120
    • View Profile
    • Donate to Member
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.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
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,120
    • View Profile
    • Donate to Member
Yep, that's the impression I'm forming as well.