1 Attachment(s)
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.
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 ?
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...
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 ?
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.
}
Re: c# form size change problem
Quote:
Originally Posted by
BigEd781
...
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. :D
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.
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);
Re: c# form size change problem
So the only question remaining is WHY? ;)
Re: c# form size change problem
Quote:
Originally Posted by
rliq
So the only question remaining is WHY? ;)
Why doubleBuffering is per sure no question :D 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 :wave:
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 ?
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.