1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
GDI Graphics In Delphi
Page 4: Drawing vs. Painting
 More of this Feature
• Page 1: GDI Jargon
• Page 2: Drawing: Lines
• Page 3: Drawing: Shapes
• Page 5: Handles and Stuff
• Page 6: Pictures: TBitmap
• Page 7: Ways To Kill Flicker
• Page 8: GDI, The Hard Way
• Page 9: API Drawings
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Graphics programming in Delphi
• Screen zooming
• Double buffering
• Win API in Delphi

   Graphics That Stay Put
The disappearance of your graphics from above highlights an important distinction in GDI graphics - the difference between "Drawing" and "Painting".

Drawing is what we did above. This simply means that you draw some graphics when you want. The graphics appear, but only until the window gets refreshed.

Painting is slightly different to drawing. Whenever a window needs redrawn, Windows sends a message. This can be handled by the "OnPaint" event. Any code that you put in the OnPaint handler will be called, and graphics drawn in OnPaint will not disappear when the form gets hidden and shown. This is the fundamental difference between drawing and painting: drawing is only temporary, while painting gets called whenever the window needs refreshed, and generally deals with redrawing the entire screen.

It's time for a simple demonstration to make the distinction obvious. Dump the following procedure into a project:

procedure TForm1.DrawSomeText;
begin
  Canvas.TextOut(10, 10, 'Some text');
end; 

This looks innocent enough. If you dump a button onto the form and call DrawSomeText from the button's OnClick handler you will notice the same problem as before - the text disappears from the form when it moves off-screen. HOWEVER, if you call DrawSomeText from the form's OnPaint event handler the text stays there (finally!). This is all it takes - just an OnPaint handler.

The difference between painting and drawing should now be obvious. Generally, we would want to deal with drawing, dumping things onto the screen in a transient way. At the same time, we could store the changes somehow. One method for this is to create a bitmap and draw the change to both the bitmap and the screen. When it's time to paint we draw the bitmap to screen, which will be up-to-date. This will be discussed in a later tutorial.

   Question, Suggestions...
If you have any questions or comments to this (huge) article, please post them on the Delphi Programming Forum. Discuss!

Next page > Using Handles and Stuff > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Articles.
More Delphi articles
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2008 About.com, a part of The New York Times Company.

All rights reserved.