Free Pascal

Glyphs for Lazarus

A glyph is a small bitmap image that can be displayed on a button, such as the TBitBtn, TSpeedButton or TEditButton.

You can set a button's glyph in the Object Inspector by clicking next to the property Glyph. This opens a dialog that lets you load a picture from a PNG, BMP, or ICO file. Lazarus automatically converts the picture to the bitmap format.

You also can set the Glyph property under program control, like this:

SpeedButton1.Glyph := SomeBitmap;

Note that SomeBitmap must be a TBitmap object, not simply a string containing the path and filename of a picture. Thus the entire code becomes something like:

SpeedButton1.Glyph := Image1.Picture.Bitmap;

A collection of glyphs

Lazarus provides some images that are suitable for glyphs, but they are scattered all over the place. So it seems like a good idea to collect a number of suitable pictures and put them in a folder, for example c:\lazarus\images\glyph.

  1. Create a new folder glyph under the existing c:\lazarus\images.
  2. Download and unzip it into c:\lazarus\images\glyph.

Glyph project

Here is a small application that shows you how to use glyphs.

  1. Start a new Lazarus project and save it as Glyph.
  2. Drop a TShellListView on the form. You find it under the tab "Misc".

    Set its properties as follows:
      Name: List
      ShowColumnHeaders: False
      ViewStyle: vsList
  3. Add a TLabel.
  4. Under the label, add a TImage.
  5. Add a TSpeedButton, a TEditButton and a TBitBtn.
  6. Select the ShellListView.
    In the Object Inspector, double click next to the event OnClick.
  7. Complete the OnClick event handler:

    procedure TForm1.ListClick(Sender: TObject);
      ImgFile: string;
      ImgFile := List.Items[List.ItemIndex].Caption;
      Label1.Caption := ImgFile;
      Image1.Picture.LoadFromFile(List.Root + '\' + ImgFile);
  8. Create an OnClick event handler for SpeedButton1:

    procedure TForm1.SpeedButton1Click(Sender: TObject);
      SpeedButton1.Glyph := Image1.Picture.Bitmap;
  9. Create an OnButtonClick event handler for EditButton1:

    procedure TForm1.EditButton1ButtonClick(Sender: TObject);
      EditButton1.Text := Label1.Caption;
      EditButton1.Glyph := Image1.Picture.Bitmap;
  10. Create an OnClick event handler for BitBn1:

    procedure TForm1.BitBtn1Click(Sender: TObject);
      BitBtn1.Glyph := Image1.Picture.Bitmap;
  11. Create an OnCreate event handler for Form1:

    procedure TForm1.FormCreate(Sender: TObject);
      List.Root := 'c:\lazarus\images\glyph';
      Label1.Caption := '...';
      SpeedButton1.Caption := 'Go';
      EditButton1.Text := '...';
      BitBtn1.Caption := 'Go';
  12. Save your files and press F9.
    Click on one of the filenames in the list.
    Click on of the buttons.

It's amazing how compact the code is, considering all the functionality of our little program.

See also:
  Download glyphs
  Glyph, part 2