TTransparentCanvas

TTransparentCanvas

An alpha-aware TCanvas-like class that allows you to compose and draw blended transparent shapes, text, etc. It also makes drawing to glass parts of a form much easier. Uses pure GDI – not GDI+, so no external libraries required. MPL license.

The TTransparentCanvas demo app - showing text, Aero text, and semitransparent shapes drawn over images and glass
The TTransparentCanvas demo app – showing text, Aero text, and semitransparent shapes drawn over images and glass

A standard VCL application running on Windows Vista or 7 can have glass areas of a form – the transparent sections showing a blurred view of the background.  This includes the title bar.  Suppose you want to draw on glass, and you’re using a version of Delphi without good (or any!) transparency support in the TCanvas class. What do you do?

You can:

  • Drop back to GDI, and, through use of AlphaBlend, BLENDFUNCTION, premultiplied alpha, upside-down (reverse-ordered) bitmaps, checking DWM composition and using DrawThemeTextEx, write code to draw Aero-style text successfully to glass.  It’s interesting stuff, but a lot of effort for a small feature.  Or…
  • You can drop this unit into your application, and use TCanvas-like methods to output text, draw shapes, etc and compose the result straight onto the glass area of your form in a few lines of code.

This unit:

  • Does not pull in any external libraries (such as GDI+) – it’s implemented purely using GDI
  • Lets you draw transparent text and shapes to a normal TCanvas (eg, to blend transparent shapes and text onto a TBitmap.)  It’s useful for implementing, say, an Explorer-like transparent selection rectangle or a closed-caption-style text label with a transparent background.
  • A custom-drawn window title bar showing text drawn with TTransparentCanvas
    A custom-drawn window title bar showing text drawn with TTransparentCanvas

    Draws Aero text (with a fuzzy background), including changing the white background color to something else – something with no support in the Windows API.  This gracefully falls back to standard text if Aero is not available, including if you call the methods on Windows XP.

  • Includes an example of drawing on the non-client area – that is, drawing on the title bar.

It’s free and licensed under the commercial-use-friendly MPL. Being open source, contributions adding more drawing functionality are gratefully accepted.