Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with follwing code:
This code is basically able to create some shape like rectangle or circle on form at runtime. Bur problem is shape is not persistent, mean clicking any where in form cause removal of shape. Please suggest to draw persistent shape using mouse event.
C#
 public partial class Form1 : Form
    {
        Rectangle shape;
        Point p1 = new Point();
        Point p2 = new Point();
       
       
       
        public Form1()
        {
            InitializeComponent();
            //this.DoubleBuffered = true;

        }


        protected override void OnMouseDown(MouseEventArgs e)
        {

            shape = new Rectangle(e.X, e.Y, 0, 0);
            p1 = e.Location;
            this.Invalidate();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                shape = new Rectangle(shape.Left, shape.Top, e.X - shape.Left, e.Y - shape.Top);

                p2 = e.Location;


            }
          this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Pen pen = new Pen(colorDialog1.Color, 1);
            
            Graphics g = this.CreateGraphics();
          
                g.DrawEllipse(pen, shape);
               
        }       
}

This is what i did. This code is capable of drawing circle on form at run time, but as i click anywhere the shape removes. I need a persistent shape.
Posted
Updated 12-Nov-13 17:08pm
v2

You need to think carefully if your goal is to create a Paint program ... one that creates raster (pixels, bitmaps) ... or if your goal is to create "graphic objects" that have persistence, can be selected, can be moved about.

For "painting" to be persistent in WinForms, you need to do all your drawing in the Paint Event of an OwnerDrawn context (container).

For "graphic object" drawing you can consider using the Visual Basic PowerPack Shape and Line features in C#, or consider creating custom shaped controls by defining Regions and clipping the visible areas of Controls, like Panels, to the Region.

Of course the two techniques can mix: you could take a Panel and make it hexagon shaped, then use the Paint Event to fill it with a gradient-fill.

Creating a Paint (raster) program in WinForms with any real functionality is a major piece of work, and I'd suggest you consider using WPF instead.

If you search CP, you will find some impressive drawing programs.
 
Share this answer
 
Comments
thatraja 12-Nov-13 10:56am    
5!
Sergey Alexandrovich Kryukov 12-Nov-13 11:28am    
All correct, good explanation, a 5.

As to WPF... well, it depends, but yes, it's worth considering. Note that one can use the same rendering approach as in Forms (having to render from the data objects created by a developer). WPF is great for vector graphics, it has nearly everything for this, and it's easy to allow the users to use their own XAML drawings to put on the drawing canvas.

I added some detail in my solution, please see.
—SA
Kalpana M 12-Nov-13 23:10pm    
Please see the new version, this is enough to draw a shape on form at runtime but i need shape should remains there.
Basically, you need to persist all graphics in your own data. Graphics is not stored anywhere. To render data, you need to override the virtual method System.Windows.Forms.Control.OnPaint or handle the event System.Windows.Forms.Control.Paint. You will get an instance of the class System.Drawing.Graphics which you can use to render your graphics. When you need modify the picture, add the changes in your data and call System.Windows.Forms.Control.Invalidate. To prevent flicker, use double buffering.

Please see also my past answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA
 
Share this answer
 
Comments
BillWoodruff 12-Nov-13 11:58am    
+5 "When he was good, he was very good indeed." (apologies to H. W. Longfellow)
Sergey Alexandrovich Kryukov 12-Nov-13 12:52pm    
Thank you for this two-edged quote. :-)
—SA
thatraja 12-Nov-13 12:19pm    
5!
Sergey Alexandrovich Kryukov 12-Nov-13 12:52pm    
Thank you, Raja.
—SA
Kalpana M 12-Nov-13 23:10pm    
Please see the new version, this is enough to draw a shape on form at runtime but i need shape should remains there.
Beware you must be refreshing what the user paints with the mouse. Check the Invalidate call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900