Programming C++Builder How to Write Graphics and Multimedia Programs in CBuilder or Delphi?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/ on line 149

How to work with graphics and multimedia in C++Builder or Delphi?

Graphics and multimedia elements can add polish to your applications. C++Builder and Delphi offers a variety of ways to introduce these features into your application. To add graphical elements, you can insert pre-drawn pictures at design time, create them using graphical controls at design time, or draw them dynamically at runtime. To add multimedia capabilities, C++Builder includes special components that can play audio and video clips.

CLX: Multimedia components are available in the VCL only.

This following topics describe how to enhance your applications by introducing graphics or multimedia elements:

  1. Overview of graphics programming
  2. Working with multimedia

A glance to the graphics programming in CBuilder or Delphi

The VCL graphics components defined in the Graphics unit encapsulate the Windows Graphics Device Interface (GDI), making it easy to add graphics to your Windows applications. CLX graphics components defined in the QGraphics unit encapsulate the Qt graphics widgets for adding graphics to cross-platform applications.

To draw graphics in a C++Builder application, you draw on an object's canvas, rather than directly on the object. The canvas is a property of the object, and is itself an object. A main advantage of the canvas object is that it handles resources effectively and it takes care of device context, so your programs can use the same methods regardless of whether you are drawing on the screen, to a printer, or on bitmaps or metafiles (drawings in CLX). Canvases are available only at runtime, so you do all your work with canvases by writing code.

VCL: Since TCanvas is a wrapper resource manager around the Windows device context, you can also use all Windows GDI functions on the canvas. The Handle property of the canvas is the device context Handle.

CLXTCanvas is a wrapper resource manager around a Qt painter. The Handle property of the canvas is a typed pointer to an instance of a Qt painter object. Having this exposed allows you to use low-level Qt graphics library functions that require QPainterH.

How graphic images appear in your application depends on the type of object whose canvas you draw on. If you are drawing directly onto the canvas of a control, the picture is displayed immediately. However, if you draw on an offscreen image such as a TBitmap canvas, the image is not displayed until a control copies from the bitmap onto the control's canvas. That is, when drawing bitmaps and assigning them to an image control, the image appears only when the control has an opportunity to process its OnPaint message (VCL) or event (CLX).

When working with graphics, you often encounter the terms drawing and painting:

  • Drawing is the creation of a single, specific graphic element, such as a line or a shape, with code. In your code, you tell an object to draw a specific graphic in a specific place on its canvas by calling a drawing method of the canvas.
  • Painting is the creation of the entire appearance of an object. Painting usually involves drawing. That is, in response to OnPaint events, an object generally draws some graphics. An edit box, for example, paints itself by drawing a rectangle and then drawing some text inside. A shape control, on the other hand, paints itself by drawing a single graphic.

The following topics describe how to use graphics components to simplify your coding.

  1. Refreshing the screen
  2. Types of graphic objects
  3. Common properties and methods of canvases
  4. Handling multiple drawing objects in an application
  5. Drawing on a bitmap
  6. Loading and saving graphics files
  7. Using the clipboard with graphics
  8. Rubber banding example

Add comment

Security code

Programming - C++Builder