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 5: Handles and Stuff
 More of this Feature
• Page 1: GDI Jargon
• Page 2: Drawing: Lines
• Page 3: Drawing: Shapes
• Page 4: Draw vs. Paint
• 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

   Using Handles and Stuff
When using Delphi, you can quite happily use the standard VCL functions (e.g. Canvas.TextOut) without worrying. However, what if you don't want the VCL graphics functions, which just wrap up the Windows API versions? Well, you can use the Windows API functions directly - but wait, they all want HDCs! What's a HDC?

Just about everything in Windows uses a "Handle". The handle is just a way of identifying your object. Windows have handles, buttons have handles, etc. Therefore, you should not be surprised to learn that all your objects have these handles as a property - for example, "MyForm.Canvas.Handle". Here's a quote from Win32.hlp, explaining handles: "An application must obtain an object handle and use this handle to examine or modify the system resource (or both). In the Microsoft® Win32® application programming interface (API), handles are usually implemented as indirect pointers, but this is not always the case."

The HDC type is a Handle to a Device Context. If you remember from the first part of this tutorial, I said that TCanvas encapsulates most of the DC functions. Well, you just use your object's Canvas Handle whenever Windows wants a HDC. Nice and easy.

Having said all that, I might have confused you. If so, here are some examples of using Windows API functions versus VCL ones. You'll see they are almost the same.

VCL WINDOWS API
Canvas.TextOut(x,y,myString); TextOut(Canvas.Handle, x, y, PChar(myString), Length(String));
Canvas.FloodFill(X, Y, Color,fsBorder); ExtFloodFill(Canvas.Handle, x, y, YourColour, FLOODFILLBORDER);
Canvas.LineTo(x,y); LineTo(Canvas.Handle, x, y);
Canvas.MoveTo(x,y); MoveToEx(Canvas.Handle, x, y, nil);

Generally, you just use the same functions as before, but pass in the appropriate handle first. Remember that you can supply different handles to draw to different places. For example, you could use "SomeBmp.Canvas.Handle" to draw to a bitmap, or "Form1.Canvas.Handle" to draw to a form.

One point to be aware of is that Windows wants strings to be passed in as null-terminated. All this means is that instead of passing in your strings directly, you typecast the string as a PChar (see TextOut(), for example). That's all! You might also be required to give the length of the string - if so, just use the Length function.

Okay, now we can draw lines and shapes (and other stuff, like text). However, it would be nice to have some bitmap drawing too...

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

Next page > Drawing Bitmaps > 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
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.