0

I have a simple Photoshop-made grid and i would like to use it as progress bar, i need to draw round ellipses from 1 to 100 (then probably about 100 times in x time).

If I use System.Graphic I have not persistent result.

Then I found the code to use the PaintEventArgs method by inserting instructions in the Paint Event of the form.

Unfortunately in my mind this is not a solution because I need to draw only when I need and only where I want.... in other word I need a simple Function able to draw desired ellipses when I need...

I tried also to override the OnPaint-base but I really don't understand how to use it and if may help to reach my goal.

Here some code:

With the paint event:

    private void Main_Paint(object sender, PaintEventArgs e)
    {
        // Create pen.
        Pen blackPen = new Pen(Color.Yellow, 3);

        // Create rectangle for ellipse.
        Rectangle rect = new Rectangle(355, 282, 9, 9);
        e.Graphics.DrawEllipse(blackPen, rect);
    } 

With the Graphic mode:

private void lbl_help_Click(object sender, EventArgs e)
{   
    //SetStatus("BURNING PROCESS COMPLETED SUCCESSFULLY");
    Graphics g = this.CreateGraphics();
    // Create pen.
    Pen blackPen = new Pen(Color.Yellow, 3);

    // Create rectangle for ellipse.
    Rectangle rect = new Rectangle(355, 282, 9, 9);

    // Draw ellipse to screen.
    g.DrawEllipse(blackPen, rect);

    System.Threading.Thread.Sleep(3000);            
}

And this one I found to override OnPaint(), but I really don't know how to use it, how to override the form-paint event or how to call it only when needed and passing values:

private void lbl_help_Click(object sender, EventArgs e)
{   
    //SetStatus("BURNING PROCESS COMPLETED SUCCESSFULLY",);
    Graphics g = this.CreateGraphics();
    // Create pen.
    Pen blackPen = new Pen(Color.Yellow, 3);

    // Create rectangle for ellipse.
    Rectangle rect = new Rectangle(355, 282, 9, 9);

    // Draw ellipse to screen.
    g.DrawEllipse(blackPen, rect);

    System.Threading.Thread.Sleep(3000);            
}

Something other other:

I imagine if I use variables to store the percentage and call a paint-refresh of the form (maybe invalidate?) to update the result should work but I will lose any sort of animation, elsewhere I come-back to a non persistent state again... I need to use the grid as a progress bar, adding circles only at desired time, without losing the back drawings...

The grid I need to fill is very simple, here a screenshot:

Sry I should not post image for the reputation (i'm a new user!), here the link

EDIT: I solved the smoothing problem (at least with the Graphic Mode):

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
WizardingStudios
  • 554
  • 2
  • 7
  • 22
  • you need to show your code because the only way to make them persist is to draw them in the Paint Event. Not sure how "when you want" plays into it because Windows decides that to some degree – Ňɏssa Pøngjǣrdenlarp Mar 15 '15 at 00:26
  • possible duplicate of [How to paint additional things on a drawn panel?](http://stackoverflow.com/questions/27505578/how-to-paint-additional-things-on-a-drawn-panel) – Peter Duniho Mar 15 '15 at 03:26
  • "draw only when I need and only where I want" -- for better or worse, you don't get to decide that. If you want to draw using the Winforms API, you have to use the API's rules. Those rules dictate that the _framework_ decides "when to draw". Your code must always be ready to draw, and it does so by responding to the `Paint` event (as a handler or overriding `OnPaint()`). If your own code changes something that causes a need to update the drawing, you call `Invalidate()` and then wait for the framework to raise the `Paint` event so that you can draw. See the duplicate question for more details. – Peter Duniho Mar 15 '15 at 03:29
  • Yes, now it's more clear for me.... The persistent is not made by the eventargs component but it's only a result of a paint event. – WizardingStudios Mar 15 '15 at 15:57
  • Of course i decided to use the create graphic method (which should be inserted in any function without the need of painteventargs). I will later intercept the paint event to prevent essential graphics to disappear. I'm really in disappoint with the .Net framework which is not able to create a persistent drawing object and force me to put all the code in the onpaint event... Thanks to all for the help! – WizardingStudios Mar 15 '15 at 16:22

0 Answers0