c# form size change problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: c# form size change problem

  1. #1
    Join Date
    Sep 2001
    Location
    Rotmanka, Poland
    Posts
    88

    c# form size change problem

    Hi All !

    I wrote a easy program: one standard form. Next I define a paint method for this form:

    Code:
     bool cl = true;
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Brush c;
                if (cl)
                     c = new SolidBrush(Color.Gray);
                else
                     c = new SolidBrush(Color.Green);
    
                cl = !cl;
    
                e.Graphics.FillRectangle(c, new Rectangle(0,0,Width,Height));
                
            }
    So by every screen redraw shoud be one time gray, one time green background. But, when I i.e. change a form size, I see that not full form is redraw everytime, that I have a "zebra" effect (look attachment)


    How to force, that always all will be redraw ?

    THNX in advance
    Olek

    p.s.
    I know, that I can a set a background property for have always same color. THis is not a problem. Problem is with paint somthink else, not only a background. I know also that I can put a Refresh at Resize, but this is also no solution for me - important is for my program not to redraw double.
    Attached Images Attached Images  

  2. #2
    Join Date
    Sep 2001
    Location
    Rotmanka, Poland
    Posts
    88

    Re: c# form size change problem

    I found a solution myself:

    SetStyle(ControlStyles.ResizeRedraw , true);

    But I still have same problem when a form is AutoScroll. At this case is making simmilare paint optimalisation under form scrolling.

    Do you know someone solution for this ?

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: c# form size change problem

    I'm assuming you have a reason for doing this, as I cannot think of one. You will always be running into problems trying to do this sort of stuff...

    Sometimes only part of the form will be invalidated (need to be repainted), so only that part will change colour (probably the issue you are having with scrolling). This may also happen when another application is in front of yours and then is moved, uncovering part of your form. This is because windows is trying to optimise it's painting process.

    There will also only ever be a maximum of *one* paint message on your apps message queue and it will be processed after all of the others, you have little control over that and hence little control over not only where, but also when the parts of the form will need to be repainted.

    You will want to make sure the *whole* of the form is invalidated each time part of it needs to be redrawn, see Form.Invalidate(). However, I think it is not advisable to call that function in the forms Paint handler.

    I suspect that there is many many more gotcha's with what you are trying to do...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: c# form size change problem

    The only point I want to mention to your code is: Dont forget to dispose the pens and brushes befoe the method is leaved. This is very necessary in any drawing cycles to dispose the drawing objects every time.

    Why do you need to pint background yourself instead of using the existing background property ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: c# form size change problem

    Jonny is right; drawing objects are not managed objects. Instead of calling dispose explicitly, I like to use a 'using' block, but it is the same thing (a 'using' block compiles to a try/finaly block with the dispose calls in the finally section):

    Code:
    using (SolidBrush brush = new SolidBrush(color))
    {
        // do stuff with brush, it will be disposed when control leaves the "using" block.
    }

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: c# form size change problem

    Quote Originally Posted by BigEd781 View Post
    ...
    Code:
    using (SolidBrush brush = new SolidBrush(color))
    {
        // do stuff with brush, it will be disposed when control leaves the "using" block.
    }
    You are totally right. I also prefer to do it with 'using' Block. But as I don't know the posters level and didn't want to explain a lot, I have only mentioned that he needs to do that, otherwise he will run into undefined troubes.

    Basically IMHO he needs to call Invalidate after each scrolling cycle, or he will have troubes. Using double buffering against flickering will be needed as well in resize cycles as in scrollcycles.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: c# form size change problem

    Yeah, double buffered is the way to go when doing your own drawing most of the time.

    Code:
    this.SetStyle(ControlStyles.ResizeRedraw | 
                  ControlStyles.AllPaintingInWmPaint | 
                  ControlStyles.OptimizedDoubleBuffer);

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: c# form size change problem

    So the only question remaining is WHY?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: c# form size change problem

    Quote Originally Posted by rliq View Post
    So the only question remaining is WHY?
    Why doubleBuffering is per sure no question So the only one is why selfpainting the background in complicated enviroments like scrollable Forms instead of using the background property. But as you can see the threads originator hasn't responded for a long ago, so IMHO this is a dead thread.
    Finished
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Apr 2006
    Posts
    220

    Re: c# form size change problem

    The thread is not dead yet, it has some last breaths left.

    Can anyone tell why did Microsoft team not make the drawing objects managed ?

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: c# form size change problem

    They are GDI objects with a thin wrapper around them. I don't know the implementation details, but it could be for performance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center