Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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:
----------------------------------------------------------
<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;
        } 
----------------------------------------------------------
 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);
                }
            }
----------------------------------------------------------
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 1-May-12 12:11pm
Comments
SAKryukov at 1-May-12 17:22pm
   
Where did you get this ControlPaint?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Shahin Khorshidnia at 2-May-12 9:06am
   
Perfect as always
SAKryukov at 2-May-12 9:57am
   
Thank you very much, Shahin.
--SA
takerman at 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.
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 186
4 Aajmot Sk 153
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100