Click here to Skip to main content
16,001,952 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one

i m new to CreateGraphics topic . i have a simple Graphics application where i draw a line by mouse clicked at 2 different points over the window . the line is drawn over the window . But my problem is when i switch over to other window n come back to my project the window is cleared . My previous drawn lines will be not there
so if u have any suggestion kindly let me know it . My codes are like this

C#
public Form1()
        {
            InitializeComponent();
        }
        Point[] pts;
        int pointcount = 0;
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (pointcount == 0)
                pts = new Point[2];

            if (e.Button == MouseButtons.Left)
            {
                if (pointcount != 2)
                {
                    pts[pointcount].X = e.X;
                    pts[pointcount].Y = e.Y;
                    ++pointcount;
                }
                else
                    pointcount = 0;

                if (pointcount == 2)
                {
                    ghx.DrawCurve(Pens.Black, pts);
                    pointcount = 0;
                }


            }
        }
        Graphics ghx;
        private void Form1_Load(object sender, EventArgs e)
        {
            ghx = CreateGraphics();
        }
Posted

You have to be able to redraw your Graphics in paint event. Thus typically, you would store your lines in a container (and also information for the line in progress).

OnPaint[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-11 23:39pm    
My 4. I don't think you correct idea is clearly explained. Please see my solution, with some extra detail.
--SA
arunrv 22-Aug-11 6:18am    
ya its fine it shows the drawn line even when i swap the window , but i need to draw line while i run the project , this is what i have copied the code .kno ...
Of course! The Form does not hold graphics; and this is done so for a good reason. The form is rendered every time part of it is invalidated, which happens when a part of form is shown when it was masked by some other window and later shown.

You need to handle the event Form.Paint or override its method Form.OnPaint. For example, place this code in your form's constructor:

C#
this.Paint += (sender, eventArgs) => {
   Graphics graphics = eventArgs.Graphics;
   //use this instance of Graphics to render the scene
};


As a rule of thumb, you should not create an instance of System.Drawing.Graphics, use the one passed to you in event arguments parameter.

Now, to change the rendered graphics (animation, any kind of interactive behavior such as in gaming or drawing application) make the rendering method (shown as anonymous method above) depending on some data structure; and make this data structure a field(s) of your form. When you change data, the rendering will change; but how to trigger re-drawing? You need to trigger sending the message WM_PAINT to the form.

This is done by calling Form.Invalidate (inherited from Control.Invalidate. You can improve performance using Invalidate with parameters (Rectangle or Region) to invalidate just some part of the scene.

—SA
 
Share this answer
 
Comments
arunrv 22-Aug-11 6:21am    
it will be better if u give some example so that i can understand

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