TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
tDBGrid: Painting Graphics for Field Values
 
   
 Posted: 30 August 2000
 
   
 
 Tested in Delphi 5
 
   
 
Audience: Beginner
 
       
   

Question: I'm using a DBGrid to display a Logical field. By default, the values appear as True or False. How can I display checkmarks instead?

Answer: You can use the OnDrawColumnCell event to paint bitmaps in the column displaying the Logical field. Here's one implementation:

procedure TForm1.DBGrid1DrawColumnCell( Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState );
var
   bmpImage   : tBitmap; // temp. bitmap written to grid canvas
   intX, intY : integer; // position of the bitmap in rectangle
begin

   if column.fieldName = 'LogicalField' then
     with DBGrid1.canvas do
     begin

        fillRect( rect );  // clear the value
        bmpImage := tBitmap.create;
        try

           // choose appropriate bitmap

           if Table1LogicalField.asBoolean then
              bmpImage.assign( imgChecked.Picture.bitmap )
           else
              bmpImage.assign( imgUnCheck.Picture.bitmap );

           // center the image (assuming it's smaller than rect)

           intX := ( ( rect.right - rect.left ) div 2 ) -
                   ( bmpImage.width div 2 );

           intY := ( ( rect.bottom - rect.top ) div 2 ) -
                   ( bmpImage.height div 2 );

           draw( rect.left + intX, rect.top + intY, bmpImage );

        finally
           bmpImage.free;  // only you can prevent resource leaks.
        end; // try
     end; // if/with

end;

Notes and Tips:

  • This particular approach requires formally instantiated tField and tColumn objects.

  • In this example, the source bitmaps are stored in non-visible Image objects on the form.

  • Note that we've assumed that the images will fit within the rectangle used by the field value; if your images are larger than this area, use StretchDraw() instead of Draw(). For more information, see Help.

  • When designing graphics for this type of solution, it helps to keep them small and to limit their color depth to 16-color palettes. This will reduce the memory they require and the time needed to paint them.

  • Note that we haven't disabled default drawing in this aproach. This means there's a slight risk that user's will see the original column contents (e.g. "True" or "False") before the image appears. However, with modern (or recent) hardware, it's a very small risk. In practice, this is an effective, though hardly elegant approach. Still, it produces the desired result.

  • A number of third-party components provide alternate ways to do this; however, many of the better ones are commercial. If your "component budget" is a little short, use this approach instead.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]