Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello!
I have a tryed from several days to solve my problem but without any result, so will ask here.

I have creage a graphic object and draw on it some shapes, like lines, rectangles etc.
I do not write any code in the Paint event and all code is just in the MouseUp, Down and Move Events.

-=The problem is=-
When i move, minimize, resize the picture, a partfrom the graphic is erased...
The same efect is when a window is on top of the program..

Here i will give you the code from my events to see what i'm doing for a line object:
----------------------------------------------------------
C#
<pre lang="xml">
private void pbx_MouseDown(object sender, MouseEventArgs e)
        {
            PenList.Color = MainForm.btnColor1.BackColor;
            PenList.Width = float.Parse(MainForm.tbxSize.Text);

            StartPoint = e.Location;
            EndPoint = StartPoint;

            MouseIsDown = true;
        } 

----------------------------------------------------------
C#
private void pbx_MouseMove(object sender, MouseEventArgs e)
       {
           EndPoint = e.Location;
           PenList.Color = MainForm.btnColor1.BackColor;
           PenList.Width = float.Parse(MainForm.tbxSize.Text);
           if (e.Button == MouseButtons.Left)
           {
               if (MainForm.btnLine.Checked && MouseIsDown == true)
               {
                                 ControlPaint.DrawReversibleLine(Picture.PointToScreen(StartPoint), Picture.PointToScreen(EndPoint), Color.Black);
                   EndPoint = new Point(e.X, e.Y);
                   ControlPaint.DrawReversibleLine(Picture.PointToScreen(StartPoint), Picture.PointToScreen(EndPoint), Color.Black);
               }
           }

----------------------------------------------------------
C#
private void pbx_MouseUp(object sender, MouseEventArgs e)
        {
            MouseIsDown = false;
            EndPoint = e.Location;
            PenList = MainForm.CustomPen;
            PenList.Color = MainForm.btnColor1.BackColor;
            PenList.Width = float.Parse(MainForm.tbxSize.Text);
       
            if (MainForm.btnLine.Checked)
            {
                Graphic.DrawLine(PenList, StartPoint, EndPoint);
            }
        }


Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 1-May-12 17:22pm    
Where did you get this ControlPaint?
--SA

1 solution

This is because you are doing it wrong. Actually, this is a pretty common mistake.

The rendering of graphics persists only if you perform it on each WM_PAINT Windows message. For this purpose, you should handle the event System.Windows.Forms.Control.Paint, or, better yet, override the virtual method System.Windows.Forms.Control.OnPaint in your derived control of form class. The message WM_PAINT will be triggered each time some area of the control needs re-painting, for example, when a part of a control was hidden under some other window. Normally, you do all the changes in graphics in some data, and when you change the data (with your mouse events or by any other action), you need to force sending the message. This is done via one of the System.Windows.Forms.Control.Invalidate methods. Sometimes, it's good to use the method Invalidate with parameters (Region or Rectangle), to invalidate just the part of the scene, which can help you to improve performance.

Also, you should not create an instance of System.Drawing.Graphics (please see my comment to the question). You should use the instance passed to your handler of the event System.Windows.Forms.Control.Paint or the virtual method System.Windows.Forms.Control.OnPaint as the event parameter.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

Please see my past answers to related questions:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 9:06am    
Perfect as always
Sergey Alexandrovich Kryukov 2-May-12 9:57am    
Thank you very much, Shahin.
--SA
[no name] 2-May-12 13:05pm    
Perfect Answer! Amazing, thank you alot. I will try to make it like you and i will post the complete solution.
Thanks again.
Sergey Alexandrovich Kryukov 2-May-12 13:56pm    
I'm very glad if it helps you.
Good luck, call again.
--SA

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