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:
Image1.Picture.LoadFromFile('c:\images\image1.bmp');
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.
- Create a new folder glyph under the existing c:\lazarus\images.
- Download glyphs.zip and unzip it into c:\lazarus\images\glyph.
Glyph project
Here is a small application that shows you how to use glyphs.
- Start a new Lazarus project and save it as Glyph.
- Drop a TShellListView on the form. You find it under the tab "Misc".
Set its properties as follows: Name: List ShowColumnHeaders: False ViewStyle: vsList
- Add a TLabel.
- Under the label, add a TImage.
- Add a TSpeedButton, a TEditButton and a TBitBtn.
- Select the ShellListView.
In the Object Inspector, double click next to the event OnClick.
- Complete the OnClick event handler:
procedure TForm1.ListClick(Sender: TObject); var ImgFile: string; begin ImgFile := List.Items[List.ItemIndex].Caption; Label1.Caption := ImgFile; Image1.Picture.LoadFromFile(List.Root + '\' + ImgFile); end;
- Create an OnClick event handler for SpeedButton1:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
SpeedButton1.Glyph := Image1.Picture.Bitmap;
end;
- Create an OnButtonClick event handler for EditButton1:
procedure TForm1.EditButton1ButtonClick(Sender: TObject);
begin
EditButton1.Text := Label1.Caption;
EditButton1.Glyph := Image1.Picture.Bitmap;
end;
- Create an OnClick event handler for BitBn1:
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
BitBtn1.Glyph := Image1.Picture.Bitmap;
end;
- Create an OnCreate event handler for Form1:
procedure TForm1.FormCreate(Sender: TObject);
begin
List.Root := 'c:\lazarus\images\glyph';
Label1.Caption := '...';
SpeedButton1.Caption := 'Go';
EditButton1.Text := '...';
BitBtn1.Caption := 'Go';
end;
- 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.
|