Solution for paint application.... RRS feed

  • Question

  • Hello to all
    I've created a paint application but i have a problem:

     When I minimize the form ,my drawn paint will be erased...

    how can i solve this problem??

    Monday, November 26, 2018 4:51 PM

All replies

  • Hello, 

    how can i solve this problem??

    Based on your description - you have to disable minimize button.

    And second question - It erased by putting anything over form - will be solved by setting Always On Top to true. 

    Sincerely, Highly skilled coding monkey.

    Monday, November 26, 2018 5:08 PM
  • >>> It erased by putting anything over form

    Yes it happens

    I need a solution using coding ....

    Your method is not a fundamental solution

    Monday, November 26, 2018 5:21 PM
  • A window will be repainted each time it is needed by Windows such as when minimizing and then restoring or when covered up and shown again. Therefore your app has to maintain a list of everything it wants to paint so it can repaint on demand.

    Right now I suspect you are drawing directly on the panel whenever the user says to draw something. That isn't going to be persisted. You're going to need to maintain a list of "shapes" to draw. We often use inheritance to make this pretty easy to implement. Each time the user "adds" something you'll add it to your list. Your paint event handler will simply enumerate the list and have each shape paint itself. Since Windows is responsible for painting you'll get notified in all the cases you need to repaint.

    Michael Taylor

    Monday, November 26, 2018 6:47 PM
  • can you show me a sample code from which that you say?
    Tuesday, November 27, 2018 3:40 PM
  • public abstract class Shape

    { public abstract void Draw ( Graphics gfx ); } public class Rectangle : Shape { //Properties specific to shape public Point TopLeft { get; set; } public Point BottomRight { get; set; } public override void Draw ( Graphics gfx ) { //Code to draw rectangle } } //In your form private readonly List<Shape> _shapes = new List<Shape>(); //To create a new shape create an instance of the type, add to the list and then draw it var newShape = new Rectangle() { TopLeft = new Point(10, 10), BottomRight = new Point(20, 20) }; _shapes.Add(newShape); Refresh(); //In your paint handler enumerate the shapes //Area should have already been erased by another method private void OnPaint ( object sender, PaintEventArgs e ) { foreach (var shape in _shapes) shape.Draw(e.Graphics); }

    The above code won't compile as is but gives you an idea of what to do. You'll need to adjust it to fit your application needs. There are some things you'll have to figure out (like how to let the user select shapes, get the shape properties, etc). But you should have already solved that to get the painting to work already.

    Michael Taylor

    Tuesday, November 27, 2018 4:00 PM