Chapter 30 Key Takeaways
-
TCanvas is the drawing surface. Coordinates start at (0,0) in the top-left, with X increasing rightward and Y increasing downward. All persistent drawing must occur inside
OnPaint. -
Drawing primitives:
MoveTo/LineTofor lines,Rectangle/RoundRectfor rectangles,Ellipsefor ellipses and circles,Polygonfor arbitrary shapes,Arc/Piefor arcs and wedges. -
TPen controls outlines (Color, Width, Style). TBrush controls fills (Color, Style). Set
Pen.Style := psClearfor no outline orBrush.Style := bsClearfor no fill. -
TColor uses BGR byte ordering internally. Use
RGBToColor(R, G, B)for custom colors, or predefined constants likeclRed,clNavy,clBlack. -
Text rendering uses
TextOut(X, Y, Text). Measure text dimensions withTextWidthandTextHeight. Control appearance throughCanvas.Fontproperties. -
Images are loaded with
TImage.Picture.LoadFromFileor created withTBitmap. Draw bitmaps on canvas withCanvas.DraworCanvas.StretchDraw. -
Charts from scratch: Pie charts use the
Piemethod with angle calculations. Bar charts use scaled rectangles. Line charts useMoveTo/LineTowith data-point markers. All require calculating scales from data to pixel coordinates. -
Custom controls: Use
TPaintBoxfor one-off drawing. Create reusable components by descending fromTGraphicControlorTCustomControland overriding thePaintmethod. -
Animation uses
TTimerfor periodic updates andInvalidateto request repaints. Double buffering (drawing to an off-screen bitmap first) prevents flicker. -
Call
Invalidate, not draw methods, when data changes. Invalidate requests a repaint; OnPaint does the actual drawing. This separation keeps the drawing consistent and avoids painting outside the paint cycle.