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.
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.......
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
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.
}
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
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
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.