Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Console - Modifying the palette and appearance of text

126 views
Skip to first unread message

Squeamizh

unread,
Sep 25, 2005, 8:49:24 PM9/25/05
to
Hi,


I have an application which draws to the console window in text mode
using the following two functions:

* WriteConsoleOutputCharacter
* WriteConsoleOutputAttribute

Is it possible to modify the palette used for text in console mode?
I.E., can I modfy the appearance of color 2 (normally green) to use a
specific set of RGB values?

Also, can I modify the appearance of the text characters?

Thank you.

Kellie Fitton

unread,
Sep 26, 2005, 12:55:54 AM9/26/05
to
Hi,

Well, if your application needs the colour changes to effects the
currently active console window, you can use the following API to
set the current foreGround and backGround colours:

SetConsoleTextAttribute()

However, I've tried the following API's to modify the console's
palette dynamically during the console's run session, and even
though all the calls have succeeded withOut any error code, the
results was not the desired effects on the colour palette.

SetConsoleTitle()
FindWindowEx()
GetDC()
GetDeviceCaps() using RASTERCAPS
CreatePalette()
SelectPalette()
RealizePalette()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setconsoletextattribute.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setconsoletitle.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindowex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_4esj.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_88s3.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/colors_4x5x.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/colors_4u79.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/colors_9kv9.asp

Hope these information helps,

Kellie.

James Brown

unread,
Sep 26, 2005, 4:25:28 AM9/26/05
to
Hi,

The standard API palette routines do not (as you have found) work
on console windows. However...

it is possible to modify a console window's palette at runtime
using an undocumented method. It uses the same "interface" to control
the console window at runtime that is used by the console's Properties
window

www.catch22.net/source/files/setconsoleinfo.c

VOID WINAPI SetConsolePalette(COLORREF crPalette[16]);

e.g.

COLORREF DefaultColors[16] =
{
0x00000000, 0x00800000, 0x00008000, 0x00808000,
0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0,
0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00,
0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
};

SetConsolePalette(DefaultColors);


James
--
www.catch22.net
Free win32 software, sourcecode and tutorials


"Kellie Fitton" <KELLIE...@YAHOO.COM> wrote in message
news:1127710554....@g14g2000cwa.googlegroups.com...

Kellie Fitton

unread,
Sep 26, 2005, 10:38:46 AM9/26/05
to
Hi James,

What about modifying the colour table in the registry? for example:
HKEY_CURRENT_USER/Console/{programName}/ColorTable00
if I use the API's RegOpenKey() and RegSetValueEx() to change the
colour table {00} to red colour(000000ff), is there a way to make
these changes affects the active console window and set the console
palette to any specific RGB values?

Kellie.

James Brown

unread,
Sep 26, 2005, 11:17:10 AM9/26/05
to
Hi Kellie,
yes the colour-table in the registry is another way to do it,
but this change does not happen at runtime. The registry settings that
are stored for particular console windows are applied only when
the console is created. After this the only way to change the settings
is to use the console's Properties dialog...when you click "Apply" for
this window, the exact same sequence of code is executed that is
contained in the "setconsoleinfo.c" file I posted. Of course when you click
Apply the settings can also be saved back into the original registry
location.

The only "runtime" programmatic method is to use the undocumented
console message (WM_USER+201) and pass a handle-to-section which
contains the CONSOLE_INFO structure.

James

--
www.catch22.net
Free win32 software, sourcecode and tutorials


"Kellie Fitton" <KELLIE...@YAHOO.COM> wrote in message

news:1127745526....@g44g2000cwa.googlegroups.com...

Squeamizh

unread,
Sep 26, 2005, 3:45:59 PM9/26/05
to
James Brown wrote:
> Hi,
>
> The standard API palette routines do not (as you have found) work
> on console windows. However...

Awesome, thank you James. I will try this later today.

Kellie Fitton

unread,
Sep 26, 2005, 4:17:01 PM9/26/05
to
Thanks James.

Kellie. :--)

Squeamizh

unread,
Sep 27, 2005, 11:25:45 AM9/27/05
to
On Mon, 26 Sep 2005 09:25:28 +0100, "James Brown"
<remove_james_dot_brown7_at_virgin_dot_net> wrote:

>Hi,
>
>The standard API palette routines do not (as you have found) work
>on console windows. However...
>
>it is possible to modify a console window's palette at runtime
>using an undocumented method. It uses the same "interface" to control
>the console window at runtime that is used by the console's Properties
>window
>
>www.catch22.net/source/files/setconsoleinfo.c
>
>VOID WINAPI SetConsolePalette(COLORREF crPalette[16]);
>
>e.g.
>
>COLORREF DefaultColors[16] =
>{
> 0x00000000, 0x00800000, 0x00008000, 0x00808000,
> 0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0,
> 0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00,
> 0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
>};
>
>SetConsolePalette(DefaultColors);
>
>
>James


Hi James,

I tried with the code you linked to, but for some reason I am not
seeing any palette change (I modified the array so the colors are no
longer the default ones). I also tried changing a few of the other
fields like window width/height, just to see if anything was
happening, but the console window is not changing at all. Your
routines are getting executed with no problems (no exceptions or
anything), but I am just not seeing any results on the screen.

Unfortunately I am quite unfamiliar with Windows programming. I
cannot easily follow your code because of this, and I am having a
difficult time debugging this. I know it's a vague and general
question, but would you happen to have any ideas on what the problem
could be? I would very much like to be able to change the console's
palette, and it would be perfect if I could get your solution to work.

If I can't get it to work, then I will try to do the registry thing
that Kellie suggested (thanks Kellie!), but ideally I would like to be
able to change the palette multiple times for the same console window.

Thanks.

James Brown

unread,
Sep 27, 2005, 12:12:55 PM9/27/05
to
firstly this tech only works for NT family of OSs.

But interestingly it appears to be not working on my XP SP2..

I've just dug around and the undoc message (0x4c9) is no longer being
sent...
this is something that has presumably changed since the related security
advisory+patch.

So things are slightly different on Windows with security update KB890859
applied

I'll see if I can find what new method is being used now and post an update.

James

www.catch22.net
Free win32 software, sourcecode and tutorials


"Squeamizh" <blhbalh@.balh> wrote in message
news:ekoij11jrhttht47u...@4ax.com...

James Brown

unread,
Sep 27, 2005, 1:14:44 PM9/27/05
to
ok just confirmed with SoftIce that the message is still being sent.

The difference is that the CONSOLE_INFO structure (as I call it)
has changed size slightly. Put the following at the end of the struct.

BYTE Padding[0x20];

I'll update the source on my site later on.

James

--

www.catch22.net
Free win32 software, sourcecode and tutorials


"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message
news:43396fec$0$22983$cc9e...@news.dial.pipex.com...

James Brown

unread,
Sep 27, 2005, 1:24:33 PM9/27/05
to
ok...new version is on the site should work ok now.

www.catch22.net/source/files/setconsoleinfo.c

James

--

www.catch22.net
Free win32 software, sourcecode and tutorials

"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message
news:43396fec$0$22983$cc9e...@news.dial.pipex.com...

carv...@gmail.com

unread,
Sep 27, 2005, 6:17:28 PM9/27/05
to
I tried the registry method, but don't seem to be getting any results.
I have one program (WININIT) that sets the registry entries for another
program (UI) as follows:

HKEY_CURRENT_USER\Console\C:_UI.EXE\ColorTable00 set to a value of
0x00bababa

It then launches C:\UI.EXE so the registry entries are there before the
program starts.

However, UI doesn't appear to see the ColorTable00 setting in the
registry. It still uses the ColorTable00 value from
HKEY_CURRENT_USER\Console.

If I go to UI Properities and manually change the value for
ColorTable00 and save, it updates the registry and overwrites WININIT's
entry so I assume it must be writing to the correct registry key.

I am using Windows XP SP1 and Visual Studio .NET C++

Any ideas why this wouldn't be working?

Rich

Squeamizh

unread,
Sep 27, 2005, 10:56:15 PM9/27/05
to
On Tue, 27 Sep 2005 18:14:44 +0100, "James Brown"
<remove_james_dot_brown7_at_virgin_dot_net> wrote:

> BYTE Padding[0x20];

It works beautifully! Thank you so much--this is a huge help.

James Brown

unread,
Sep 29, 2005, 7:07:50 AM9/29/05
to
<carv...@gmail.com> wrote in message
news:1127859448....@g49g2000cwa.googlegroups.com...

The registry settings for a console are based upon the console-window title.
So unless you start the console-program with the same title that appears
in the registry, it will not pick up the correct settings.

This is how the "Apply to current window only" and "Save properties for
future windows with same title"
thing works when you change the console properties using the regular
Properties dialog.

i.e. when a console is created, it checks it's title, and then tries to
match this title
to one stored in the registry under the HKCU\Console tree.

Squeamizh

unread,
Oct 1, 2005, 4:31:09 PM10/1/05
to
On Tue, 27 Sep 2005 18:14:44 +0100, "James Brown"
<remove_james_dot_brown7_at_virgin_dot_net> wrote:

>ok just confirmed with SoftIce that the message is still being sent.
>
>The difference is that the CONSOLE_INFO structure (as I call it)
>has changed size slightly. Put the following at the end of the struct.
>
> BYTE Padding[0x20];
>
>I'll update the source on my site later on.
>
>James


Hi James,

I have another very similar question. The code that you provided
works perfectly for consoles that are not in full-screen mode.
However, I epxerience problems when trying to change the palette of a
full-screen console. If I specify FullScreen = TRUE in the
ConsoleInfo struct, calling the SetConsoleColor function results in
the screen flickering for a moment (as if the console switched to
windowed mode and then instantly back to full-screen mode again), and
the palette remains unchanged. After this, I have to manually
ALT+ENTER to see the change in palette (switching back to full-screen
mode once again after doing this resulst in a full-screen console with
the correctly modified palette).

What I would like now is to be able to instantly change the palette of
a full-screen console. I have the sinking suspicion that this is not
possible, but I thought I would ask in case you or anyone else happens
to know of a solution. If not then I can definitely make do with what
you've given me.

Thanks again for your help so far,
Chris

Squeamizh

unread,
Oct 1, 2005, 11:26:40 PM10/1/05
to
On Sat, 01 Oct 2005 13:31:09 -0700, Squeamizh <blhbalh@.balh> wrote:

>Hi James,
>
>I have another very similar question. The code that you provided
>works perfectly for consoles that are not in full-screen mode.

>However...

Actually, my mistake. I won't be needing to do this afterall.

James Brown

unread,
Oct 2, 2005, 6:09:45 AM10/2/05
to
"Squeamizh" <blhbalh@.balh> wrote in message
news:nokuj1pnpble69tru...@4ax.com...

that's good because I'm not sure how to set the console when it's in
full-screen mode. Windows puts it in some kind of video emulation mode and
the palette-change occurs (I believe) through an LPC call (i.e. a
CSRSS message, not a Windows message).

0 new messages