RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomImageList.Draw Method (TCanvas, Integer, Integer, Integer, Boolean)

Draws the image specified by the Index parameter onto the provided Canvas.

Pascal
procedure Draw(Canvas: TCanvas; X: Integer; Y: Integer; Index: Integer; Enabled: Boolean = True); overload;
procedure Draw(Canvas: TCanvas; X: Integer; Y: Integer; Index: Integer; ADrawingStyle: TDrawingStyle; AImageType: TImageType; Enabled: Boolean = True); overload;
C++
__fastcall Draw(TCanvas Canvas, int X, int Y, int Index, Boolean Enabled = True);
__fastcall Draw(TCanvas Canvas, int X, int Y, int Index, TDrawingStyle ADrawingStyle, TImageType AImageType, Boolean Enabled = True);

Use the Draw method to draw one of the images in the image list onto a specified canvas. 

Canvas is the drawing surface on which to render the image. 

X and Y specify the location where the top-left corner should appear on Canvas. 

Index indicates which image to draw, where 0 specifies the first image, 1 specifies the second image, and so on. 

ADrawingStyle indicates how the color of the image may be altered. If this parameter is not specified, Draw uses the value of the DrawingStyle property. 

AImageType indicates whether to draw the image or its associated mask. If this parameter is not specified, Draw uses the value of the ImageType property. 

Enabled indicates whether the image appears as a gray-mapped image. If Enabled is false, Draw renders a gray-mapped version of the image. Because Enabled has a default value of true, this parameter can be omitted if the image should not be grayed.  

C++ Examples: 

 

/*
The following code uses the bitmaps in an image list
component to draw the contents of each cell in a draw grid.
It draws a focus rectangle around the cell that has focus.
goDrawFocusSelect in the DrawGrid Options parameter must be
True to set focus on a cell.  The ImageList Draw method must
be called after DrawFocusRect. The OnSelectCell event handler
must implemented to return true.
*/
void __fastcall TForm1::DrawGrid1DrawCell(
  TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
  long index = ARow * DrawGrid1->ColCount + ACol;
  DrawGrid1->Canvas->Brush->Color = clBackground;
  DrawGrid1->Canvas->FillRect(Rect);
  if (State.Contains(gdFocused))
  {
    DrawGrid1->Canvas->DrawFocusRect(Rect);
    Label1->Caption = "Cell " + IntToStr(int(index)) + " has the focus.";
  }
  ImageList1->Draw(
    DrawGrid1->Canvas, Rect.Left, Rect.Top, index, True);
}

void __fastcall TForm1::DrawGrid1SelectCell(TObject *Sender, int ACol, int ARow,
      bool &CanSelect)
{
  CanSelect= True;
}
/*
The following code is the OnDrawPanel event handler of a
status bar.  It draws each panel of the status bar, adding
text and icons stored in ImageList1. The image list contains
as many icons as there are header sections.  This example
requires a populated image list and a statusbar with several
panels added to the Panels property.  Select each panel and
set the Style property to psOwnerDraw.
*/
void __fastcall TForm1::StatusBar1DrawPanel(TStatusBar *StatusBar,
        TStatusPanel *Panel, const TRect &Rect)
{
  TCanvas *canvas = StatusBar->Canvas;
  canvas->Brush->Color = clRed;
  canvas->FillRect(Rect);
  canvas->Font->Color = clYellow;
  ImageList1->Draw(canvas,Rect.Left,Rect.Top, Panel->Index, true);
  canvas->TextOut(Rect.left + 30, Rect.top + 2, "Panel" + IntToStr(Panel->Index));
}

 

Delphi Examples: 

{
The following code uses the bitmaps in an image list
component to draw the contents of each cell in a draw grid.
It draws a focus rectangle around the cell that has focus.
goDrawFocusSelect in the DrawGrid Options parameter must be
True to set focus on a cell.  The ImageList Draw method must
be called after DrawFocusRect. The OnSelectCell event handler
must implemented to return true.
}
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  index: Integer;
begin
  index := ARow * DrawGrid1.ColCount + ACol;
  DrawGrid1.Canvas.Brush.Color := clWhite;
  DrawGrid1.Canvas.FillRect(Rect);
  if (gdFocused in State) then
  begin
    DrawGrid1.Canvas.DrawFocusRect(Rect);
    Label1.Caption:= 'Cell ' + InttoStr(index) + ' has the focus.';
  end;
  ImageList1.Draw(DrawGrid1.Canvas,Rect.Left,Rect.Top,index, True);
end;

procedure TForm1.DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  CanSelect:= True;
end;
{
The following code is the OnDrawPanel event handler of a
status bar.  It draws each panel of the status bar, adding
text and icons stored in ImageList1. The image list contains
as many icons as there are header sections.  This example
requires a populated image list and a statusbar with several
panels added to the Panels property.  Select each panel and
set the Style property to psOwnerDraw.
}
procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
  Panel: TStatusPanel; const Rect: TRect);
begin
with StatusBar1.Canvas do
  begin
    Brush.Color := clRed;
    FillRect(Rect);
    Font.Color := clYellow;
    ImageList1.Draw(StatusBar1.Canvas,Rect.Left,Rect.Top,Panel.Index);
    TextOut(Rect.left + 30, Rect.top + 2, 'Panel' + IntToStr(Panel.Index));
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!