Solved

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

Posted on 2004-04-08
8
257 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 45
Coding C# in Linux 8 66
Install Problem 13 30
Graphics 2 28
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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