fguihen Posted March 14, 2005 Posted March 14, 2005 i have overridden the onPaint method, and incorporated doubble buffering and i still have a flicker in my app. im drawing everything through my own draw method, and i am calling this through a thread. i cant get rid of the flicker, but i can reduce it if i play around with the sleep duration of the thread. any ideas anyone? Quote
Administrators PlausiblyDamp Posted March 14, 2005 Administrators Posted March 14, 2005 How are you calling the OnPaint method from the other thread? You need to be very careful causing UI updates from anything other than the applications main thread. IIRC the recomended way to repaint is to invalidate the region in question using the form's Invalidate method. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fguihen Posted March 14, 2005 Author Posted March 14, 2005 How are you calling the OnPaint method from the other thread? You need to be very careful causing UI updates from anything other than the applications main thread. IIRC the recomended way to repaint is to invalidate the region in question using the form's Invalidate method. im fairly sure that im not calling the on paint method from any other thread. i only set up one thread and this is the only place the my "draw " method is being called from. i have overriden the forms OnPaint, and "OnPaintBackground" methods but still no joy. Quote
Talyrond Posted March 14, 2005 Posted March 14, 2005 Are you using the graphics object supplied by the OnPaint method? and not the dreaded CreateGraphics! Quote
fguihen Posted March 14, 2005 Author Posted March 14, 2005 I am using a Graphics object from the CreateGraphics object!? why is this bad? i dont know how to get a graphics object any other way. heres how im doing it: Graphics e = this.CreateGraphics(); this.DrawToScreen(e); can you advise me of a better way? Quote
Talyrond Posted March 14, 2005 Posted March 14, 2005 Hi fguihen, CreateGraphics is not bad, it has it�s uses but for drawing on double buffered controls, it�s ain�t no help! A bit of bedtime reading for you: http://www.bobpowell.net/creategraphics.htm OK C#, I�m a VB man so I will try and use C#, hope the syntax is correct! protected override void OnPaint(PaintEventArgs e) { this.DrawToScreen(e.Graphics); } As you have overridden the OnPaint method I assume you are doing all your drawing here? If so (if not this is where you should be!) as you can the graphics object supplied by OnPaint, is being passed to your draw method. Let me know how you get on Talyrond Quote
fguihen Posted March 15, 2005 Author Posted March 15, 2005 i cant use that way im afraid. you see im using my own draw method within a thread so that i can use buttons like pause to stop my simulation. without the thread , they wont work, and to use a thread, i cant use onPaint, so i cant get a graphics objet from the OnPaint Method. any other ideas? Quote
thenerd Posted March 15, 2005 Posted March 15, 2005 This is how I always draw, it gets rid of the flickering and I just happen to like it. Have two graphics objects, one of them .creategraphics and one of them graphics.fromimage(buffer) then make a bitmap named buffer, make it = a new bitmap(width,height). do all your drawing and clearing in your own draw sub. Draw using your graphics object that's from the image. then, at the end of the sub, simply have the creategraphics draw buffer to the form, never clear the form either, just have creategraphics overdraw it over and over again. Quote
Talyrond Posted March 16, 2005 Posted March 16, 2005 Hi fguihen again, I do not have any experience with threads, but could you have a variable that has global scope, that�s shared in VB is that static in C#? not sure. Could you set this variable in the onPaint and pass the global graphics variable to your draw method, just an idea. Another thing have you got flicker free drawing without threading? It would be a good starting point and make sure that you don�t have a problem else where? Quote
Cags Posted March 17, 2005 Posted March 17, 2005 Just thought i'd mention that often if you override the onpaint method and you are drawing the entire control, you can normally override the onpaintbackground (leaving it blank). Not doing this can sometimes cause flickering. I know you said you had overriden the onpaintbackground but you didn't say if you had blanked taken out the base.OnPaintBackground (pevent); Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.