By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

Erasing one or more drawn characters...

P: n/a
I'm drawing text using the DrawString method. I need a way to go back and
erase one or more characters. As a first thought it seemed that one way to
do it would be to go back to the character(s) I want to erase and merely
re-write the same character(s) using a brush whose foreground color is the
same as the background color. The following code does that:

protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Brush brush = new SolidBrush(ForeColor);
Brush eraser = new SolidBrush(BackColor);

Point pt = AutoScrollPosition;

grfx.DrawString("ABC", Font, brush, pt.X, pt.Y);
grfx.DrawString("ABC", Font, eraser, pt.X, pt.Y);
grfx.DrawString("DEF", Font, brush, pt.X, pt.Y);
}

Although it works somewhat, there seems to be a "shadow" of some sort after
I replace ABC with DEF. I don't know if it's because the rewrite of ABC
using the background color doesen't exactly line up with the first write, or
something else. In any case, it seems that there must be a better approach.
As usual, thanks for your help in advance.

Ray
Jun 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mon, 18 Jun 2007 17:16:00 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
Although it works somewhat, there seems to be a "shadow" of some sort
after
I replace ABC with DEF. I don't know if it's because the rewrite of ABC
using the background color doesen't exactly line up with the first
write, or
something else. In any case, it seems that there must be a better
approach.
As usual, thanks for your help in advance.
I think it's likely that if you turned off anti-aliased drawing, you might
get it to work better. Whether it would perfectly or not, I can't say,
but I think it would work better. Alternatively, you could measure the
string you want to erase and just use a FillRectangle() on top of it with
the appropriate brush matching the background.

That said, I think that if you have the logic to identify that you've
drawn a character that you don't want and can go back and draw it again in
a different color, that you would be better off fixing the logic to not
draw the character in the first place. I don't believe that most
applications do not go around trying to erase things that they've already
drawn. I know I've never written one that does that, and I've worked on a
lot of GUI applications in my time.

Pete
Jun 19 '07 #2

P: n/a


"Peter Duniho" wrote:
On Mon, 18 Jun 2007 17:16:00 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
Although it works somewhat, there seems to be a "shadow" of some sort
after
I replace ABC with DEF. I don't know if it's because the rewrite of ABC
using the background color doesen't exactly line up with the first
write, or
something else. In any case, it seems that there must be a better
approach.
As usual, thanks for your help in advance.

I think it's likely that if you turned off anti-aliased drawing, you might
get it to work better. Whether it would perfectly or not, I can't say,
but I think it would work better. Alternatively, you could measure the
string you want to erase and just use a FillRectangle() on top of it with
the appropriate brush matching the background.

That said, I think that if you have the logic to identify that you've
drawn a character that you don't want and can go back and draw it again in
a different color, that you would be better off fixing the logic to not
draw the character in the first place. I don't believe that most
applications do not go around trying to erase things that they've already
drawn. I know I've never written one that does that, and I've worked on a
lot of GUI applications in my time.

Pete
Hi Pete,

Thanks for the reply. However, this is still part of the backspace issue I
posted about before, so by the time I find out I don't need the character(s),
I've already written them! I'll try your other suggestions, though.

Thanks,
Ray
Jun 19 '07 #3

P: n/a
On Mon, 18 Jun 2007 17:44:27 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
Thanks for the reply. However, this is still part of the backspace
issue I
posted about before, so by the time I find out I don't need the
character(s),
I've already written them! I'll try your other suggestions, though.
On the face of it, that doesn't make much sense. Typically, an
application will maintain a data structure that represents the data to be
drawn. When the Paint event occurs, the application uses that data to
redraw the areas of the screen that need to be redraw as indicated by the
invalidate region of the window. In many cases, an application will even
just redraw everything, counting on the fact that since the actual drawing
is clipped to the invalidated region, performance doesn't suffer very much
from drawing a bunch of stuff that doesn't wind up going to the screen
(this is even more true today, but even a decade or two ago, relative to
what applications were drawing, it was still usually true).

I infer then from your response that you are somewhere maintaining a
bitmap that represents the text you want to draw, and that when the
backspace is signaled, you have to go back and remove the character
already drawn. However, it seems to me that this isn't necessarily an
efficient way to store the data (you could just store the text), and of
course your need to erase a portion of it is causing a problem.

Had no problems come up, I don't think I'd be too concerned about using a
bitmap to store the window's image. But since it sounds like it's that
very design that is causing the issues, you may want to consider an
alternate means of drawing your window. Such as storing the data as text,
simply removing the relevant character when the backspace is processed,
and when drawing, just draw all of the text (or if you like, constrain
your drawing to just that text that would fit in the window, if you expect
the total amount of text to wind up unreasonably large).

You might be able to fix the problem by using one of the other suggestions
I provided. But note that turning off anti-aliased drawing will reduce
the quality of your text output, and using FillRectangle() to erase the
text may result in problems in the future, depending on how accurately the
text measurement is done, whether you wind up having other graphics with
the text (underlining, highlighting, whatever), and even whether the text
is kerned (characters overlapping each other's display rectangle).

I think it would be better to fix your design so that erasing what you've
already drawn is not necessary. :)

Pete
Jun 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.