Solved

GDI+ : Trying to use Invalidate() to erase several line segments before writing new line segments.

Posted on 2004-04-08
8
255 Views
Last Modified: 2010-04-15
I have an x,y coordinate system where I draw the x axis and y axis in my Form1_Paint method.  Another method is setup called PainMe() that is responsible for drawing the line segments.  I have a button that uses a for loop to simply send the x and y parameters for each line segment to PaintMe() however I have not been able to successfully use Invalidate() to erase the lines so I can write new line segments.  Here is the following code:

/********************************
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
      this.Text="Demo";
      Graphics g = e.Graphics;
      Font vertFont = new Font("Verdana", 10, FontStyle.Bold);
      Font horzFont = new Font("Verdana", 10, FontStyle.Bold);
      SolidBrush vertBrush = new SolidBrush(Color.Black);
      SolidBrush horzBrush = new SolidBrush(Color.Blue);
      Pen blackPen = new Pen(Color.Green, 2);
      Pen bluePen = new Pen(Color.Green, 2);
      // Drawing a vertical and a horizontal line
      g.DrawLine(blackPen,this.Width/2, this.Height/2,this.Width/2, this.Height/2-this.Height/2);
      g.DrawLine(blackPen,this.Width/2, this.Height/2,this.Width/2,this.Height/2+this.Height/2);
      g.DrawLine(bluePen,this.Width/2,this.Height/2,this.Width/2+this.Width/2,this.Height/2);
      g.DrawLine(bluePen,this.Width/2,this.Height/2,this.Width/2- this.Width/2,this.Height/2);
          
      // Dispose
      vertBrush.Dispose();
      horzBrush.Dispose();
      blackPen.Dispose();
      bluePen.Dispose();
      OriginX = this.Width/2;
      OriginY = this.Height/2;
}
      private void PaintMe( Graphics g, int L1eX, int L1eY, int L2eX, int L2eY, int L3eX, int L3eY, int L4eX, int L4eY )
{
      Pen L1Pen = new Pen(Color.Red,2);
      Point L1Start = new Point(OriginX, OriginY);
      Point L1End = new Point(L1eX, L1eY);
      Pen ellipse1Pen = new Pen(Color.Red);
      
      Pen L2Pen = new Pen(Color.Blue,2);
      Point L2Start = new Point(L1eX, L1eY);
      Point L2End = new Point(L2eX, L2eY);
      Pen ellipse2Pen = new Pen(Color.Blue);
      Pen L3Pen = new Pen(Color.Orange,2);
      Point L3Start = new Point(L2eX, L2eY);
      Point L3End = new Point(L3eX, L3eY);
      Pen ellipse3Pen = new Pen(Color.Orange);
      Pen L4Pen = new Pen(Color.Black,2);
      Point L4Start = new Point(L3eX, L3eY);
      Point L4End = new Point(L4eX, L4eY);
      Pen ellipse4Pen = new Pen(Color.Black);
      g.DrawLine(L1Pen, L1Start, L1End);
      g.DrawEllipse(ellipse1Pen, L1eX-2, L1eY-2, 4, 4);
      g.DrawLine(L2Pen, L2Start, L2End);
      g.DrawEllipse(ellipse2Pen, L2eX-2, L2eY-2, 4, 4);
      g.DrawLine(L3Pen, L3Start, L3End);
      g.DrawEllipse(ellipse3Pen, L3eX-2, L3eY-2, 4, 4);
      g.DrawLine(L4Pen, L4Start, L4End);
      g.DrawEllipse(ellipse4Pen,L4eX-2, L4eY-2, 4, 4);
            
}


private void button2_Click(object sender, System.EventArgs e)
{
      System.Windows.Forms.Button b = (System.Windows.Forms.Button) sender;
      
      for(int i = 1; i <= 10; i++)
      {
                      this.Invalidate(this.ClientRectangle);
            PaintMe(b.Parent.CreateGraphics(),600+i, 400+i, 200+i, 200+i, 300+i, 300+i, 400+i, 400+i);
      }
}

**************************/

When the above is executed, Invalidate() does not erase the line segments before calling PainMe().

0
Comment
Question by:int200
  • 2
  • 2
  • 2
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10789499
I tried to reproduce your problem and got another effect: lines appear for a short time and erased. Possibly this depends on the form style.

However, your approach is wrong because all drawing code should be in Form1_Paint function or called from it. What do you try to do? Animation? Do you want to see all lines at the same time or only last line?
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 10790138
Invalidate will cause the following:
-After a short time of delay WindowUpdate is called
-The specified region is erased
-The paint handler is called (i guess it's "Form1_Paint")

Note the painting outside of the paint event (such as on button click) should be consistent with the painting in the OnPaint event handler!
There are two methods to achieve this:
- Either paint only in OnPaint. Set private variables to control what is painted in the next OnPaint event. You can force OnPaint to be called using Invalidate, Update (calles OnPaint for regions which are marked as invalid before), Refresh (calls Invalidate and Update).
- Or you also set private variables that control what is painted in the next paint event but instead of forcing a paint event you paint the same outside of the paint event. Note: If you paint something different it will be lost when you drag a window over your form.
0
 

Author Comment

by:int200
ID: 10871274
I just got back from a trip and apologize for not responding to your answers before this.  I just raised the points since I'm really looking for the 'correct' way to do what I submitted above.  I simply want to erase what was drawn and draw the new line segments.

For example, I start with 4 line segments the first connected at the origin and the rest connected to each other. I would like to either programmatically or via a button click erase the line segments that are drawn and then draw new line segments on the form.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:AlexFM
ID: 10871395
You didn't answer to our questions. All drawing code should be in Form1_Paint function (ot called from it). Why do you draw outside of Paint function?
0
 

Author Comment

by:int200
ID: 10872623
If possible, I try to write my code as modular as possible using component oriented programming practices.  In this case I am trying to write code that controls the rendering of the line segments more modular. I am writing an application to model real time data (around 10Hz to 25Hz) and need control of when I erase and redraw different line segments.  If you could show me how to get this sort of control from the Form1_Paint method it would be very helpful.
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 500 total points
ID: 10873097
Well, to answer your question how to clear the drawing surface: Call FillRect and use a white SolidBrush.
If you do this you will probably experience flickering. You could get over that using double buffer but that would make your application slow again.
So we really need more information about how big the drawing surface is. What kind of graphic your displaying. What should happen if your window's size is changed. Tell us how it shall behave and what it shall display and we can help to work it out.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now