473,507 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Erasing one or more drawn characters...

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
3 2080
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


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10177
by: Owen Brydon | last post by:
Hi, Is the following code legal? g++ 3.2 barfs on it, although it seems fine to me. #include <map> using std::map; int main()
5
4889
by: Eric | last post by:
Is it possible? I'm trying to add to the current page -- but add at a specific point. Can add HTML tags from javascript midway through the page (at a place I specify) without erasing the existing...
22
2330
by: Colin McGuire | last post by:
I apologize for posting yet another scrollbar question. Here is my code. All I want is for a diagonal line to appear from coordinates (0,0) to (width,height) in a usercontrol regardless of whether...
2
1172
by: Rachel | last post by:
Hi All: I am having a problem with visual studio automatically erasing my code. I thought it was a fluke at first, but it KEEPS HAPPENING. I have a string variable for a data connection that I...
11
4326
by: eeykay | last post by:
Hello, I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the...
5
12010
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
0
3763
by: masterjuan | last post by:
Networks Hacking (hack C:/ drives, severs...)and security holes all on my website & hacking commands and I explain ways of erasing your tracks so you dont get caught doing "bad" things... What do...
2
1249
by: jonosborne | last post by:
Hi, I need to convert a text field into a number in order to perform calculation with it. However the data in the field has characters in like this: 24 hrs 3456 hrs 48 hrs etc. Is there...
4
7213
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.