home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / GLYPHLST / Main.pas < prev   
Pascal/Delphi Source File  |  1998-02-13  |  4KB  |  117 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Buttons, Grids;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     PathLabel: TLabel;
  12.     BitBtn1: TBitBtn;
  13.     GlyphList: TStringGrid;
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  16.     procedure GlyphListDrawCell(Sender: TObject; Col, Row: Integer;
  17.       Rect: TRect; State: TGridDrawState);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     { Public declarations }
  22.   end;
  23.  
  24. var
  25.   MainForm: TMainForm;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30.  
  31. const glyphPath =
  32.   'C:\Program Files\Borland\Delphi 3\Images\Buttons';
  33.  
  34. // Reads all glyph bitmaps and stores their filenames and
  35. // images in the GlyphList TStringGrid object.
  36. procedure TMainForm.FormCreate(Sender: TObject);
  37. var
  38.   SearchRec: TSearchRec;  { Directory scan result record }
  39.   K: Integer;             { while-loop control variable }
  40.   Bitmap: TBitmap;        { Holds bitmaps. Do not Free! }
  41.   Index: Integer;         { TStringGrid cell index }
  42. begin
  43.   Show;  { Make form visible while loading bitmaps }
  44.   Screen.Cursor := crHourGlass;  { Show hourglass cursor }
  45.   Index := 0;
  46.   try
  47.     PathLabel.Caption := glyphPath; { Show path above ListBox }
  48.     { Start scan }
  49.     K := FindFirst(glyphPath + '\*.*', faAnyFile, SearchRec);
  50.     try
  51.       while K = 0 do  { Scan directory for file names }
  52.       begin
  53.         if SearchRec.Name[1] <> '.' then {No '.' or '..' paths}
  54.         begin
  55.           Bitmap := TBitmap.Create;  { Create bitmap object }
  56.           try  { Get bitmap and load from list }
  57.             Bitmap.LoadFromFile(glyphPath + '\'
  58.               + SearchRec.Name);
  59.             if Index = GlyphList.RowCount then  // Expand list
  60.               GlyphList.RowCount := Index + 1;
  61.             GlyphList.Cells[0, Index] := SearchRec.Name;// Name
  62.             GlyphList.Objects[1, Index] := Bitmap;    // Bitmap
  63.             inc(Index);
  64.           except
  65.             Bitmap.Free; { Executed if ANYTHING goes wrong }
  66.             raise;       { Pass any exceptions up call chain }
  67.           end;
  68.         end;
  69.         K := FindNext(SearchRec);  { Continue directory scan }
  70.       end;
  71.     finally
  72.       FindClose(SearchRec);
  73.     end;
  74.   finally
  75.     Screen.Cursor := crDefault;  { Restore normal cursor }
  76.   end;
  77. end;
  78.  
  79. // Frees memory occupied by all glyph bitmaps. The TStringGrid
  80. // object does NOT do this automatically.
  81. procedure TMainForm.FormClose(Sender: TObject;
  82.   var Action: TCloseAction);
  83. var
  84.   I: Integer;
  85. begin
  86.   for I := 0 to GlyphList.RowCount - 1 do
  87.     TBitmap(GlyphList.Objects[0, I]).Free;
  88. end;
  89.  
  90. // Draw each glyph bitmap. The TStringGrid object is smart
  91. // enough to draw its filename text objects with no further
  92. // help. This code draws only the bitmaps in the second column.
  93. procedure TMainForm.GlyphListDrawCell(Sender: TObject;
  94.   Col, Row: Integer; Rect: TRect; State: TGridDrawState);
  95. var
  96.   Bitmap: TBitmap;
  97. begin
  98.   if col = 1 then  // Be sure to refer to the bitmap column
  99.   begin
  100.     { Get bitmap object }
  101.     Bitmap := TBitmap(GlyphList.Objects[1, Row]);
  102.     if Bitmap <> nil then
  103.     begin  { Draw bitmap in column cell }
  104.       GlyphList.Canvas.BrushCopy(
  105.         Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  106.           Bitmap.Height), Bitmap,
  107.           Bounds(0, 0, Bitmap.Width, Bitmap.Height), clRed);
  108.       { The preceding clRed argument gives the transparent
  109.         glyph substance. Change this to any solid color, or
  110.         change it to 0 to see why this is necessary. }
  111.     end;
  112.   end;
  113. end;
  114.  
  115. end.
  116.  
  117.