Displaying Multicolored text in a control

  • Thread starter Thread starter ChrisUttenreither
  • Start date Start date
C

ChrisUttenreither

Hello folks. I'm writing a Roguelike game in C#. Fun stuff. The game
includes a map that is made entirely of colored ASCII art in a
monospace font. I would like to make this map a control in the main
form.

My first attempt was with RichTextBox. I wanted to set the background
to black. After cruising newsgroups I determined that wasn't possible.

My second attempt involves an AxSHDocVw.AxWebBrowser control. I got
this by adding the "Microsoft Web Browser" control to my Toolbox. This
control does not play nicely with the rest of my form! It constantly
steals focus for itself, and there isn't a reliable mechanism for
forcing my main form to have focus. Since that control is not set up
to handle keyboard events (this is how the player moves through the
map) other people on newsgroups have been told to write an unmanaged
DLL that snags a (global?) hook and signals my application when
keyboard input has been entered.

That seems awfully complicated. A wise software engineer once told me
when a solution seems inordinately complex that I should ask if there's
a better solution. So that's the purpose of my message. :) I want to
display multicolored text in a form control. I want to be able to
change the background as well as the foreground, and I want to be able
to acquire keyboard input without writing a bunch of low level code.
What is the best solution? What is the best control to use? I am
using .NET 1.0.

Thanks!
 
Er, why the insistence on using a character-based control?

It'll be much easier and more flexible to use some blank generic
control (like a Panel) and do the text rendering with
e.Graphics.DrawString within that control's Paint event.

Eq.
 
Your first attempt was the correct one. Set the BackgroundColor of the
RichTextBox to black. You can then set the color of any text by using the
SelectionColor property when adding the text.

Set the SelectionColor property and then use AppendText to add some text in
that color. Reset the SelectionColor property and use AppendText to add some
more text in the new color. Etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.
 
Thanks to all for responding.

Paul - that's an interesting solution, one I didn't know was possible.
I'll play around with it.

Tasos, Kevin - I forgot to mention that I have the RichTextBox's
Enabled property set to false. This is what causes the control's
background to always be gray. Even if you set it after
InitializeComponent()! If The Enabled property is true, then an
annoying cursor shows up. Is there a way to force an non-Enabled
RichTextBox's background color to be black instead of gray? Another
possible solution: is there a way to get rid of the cursor? Setting
the control's ReadOnly Property to True does not appear to work.

Thanks again.
 
Paul - that's an interesting solution, one I didn't know was
possible. I'll play around with it.

If you have trouble getting it to work, you can mail me and I'll try
to send you an example. It should be much easier to do things like
scrolling if you have total control over what gets painted.
Tasos, Kevin - I forgot to mention that I have the
RichTextBox's Enabled property set to false. This is
what causes the control's background to always be gray.

Well, if you really want to use a RichTextBox, you could leave it
enabled but handle the KeyPress (and similar) events with special code
to prevent selections, deletions, insertions, and so on.

Eq.
 
Chris,

Have you tried inheriting from the RichTextBox class, and overriding the
OnPaintBackground method?

That should let you set the background colour to whatever you want.


Paul
 
Thanks Paul. I extended RichTextBox into a new class called
MapDisplayTextBox. The default constructor calls the base contructor.
Afterwards it sets Enabled to false (so that there is no carat) and
BackColor to Color.Black. Unfortunately this has not worked. I double
checked that I'm instantiating a MapDisplayTextBox instead of a
RichTextBox. I also extended some other methods to experiment and
investigate.

Here's my class:

public class MapDisplayTextBox : System.Windows.Forms.RichTextBox
{
public MapDisplayTextBox() : base()
{
this.Enabled = false;
this.BackColor = Color.Black;
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{ MessageBox.Show( "OnPaintBackground called, color is " +
this.BackColor.ToString() ); }

protected override void OnBackColorChanged(EventArgs e)
{ MessageBox.Show( "OnBackColorChanged called, color is " +
this.BackColor.ToString() ); }

protected override void OnPaint(PaintEventArgs e)
{ MessageBox.Show( "OnPaint called, color is " +
this.BackColor.ToString() ); }
}

When the exe is invoked, I get one messagebox that indicates
OnBackColorChanged was called and the color is [Black]. However when
the form is displayed the background is gray.

Maybe there is some kind of behavior coded somewhere else for
nonEnabled forms?
 
Back
Top