Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-08
8
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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