Using the Font Dialogue

For our Lazarus demonstration we added to the form only a FontDialog (from the Dialogs tab of components) and a memo (from the Standard tab).

Output from program FontDialogDemo

Output from program FontDialogDemo

Our code of the files needed for program FontDialogDemo follows. You can copy the code if necessary, but a you should be able to adapt it for your own use by inspecting the code. Again, it is the Execute function of the dialogue that is vital.

uFontDialogDemo.pas

unit uFontDialogDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TfrmFont = class(TForm)
    FontDialog1: TFontDialog;
    memFont: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure memFontClick(Sender: TObject);
  end; 

var
  frmFont: TfrmFont;

implementation

{$R *.lfm}

procedure TfrmFont.FormCreate(Sender: TObject);
begin
  memFont.Lines.text := 'Access the font dialogue by clicking in this memo';
end;

procedure TfrmFont.memFontClick(Sender: TObject);
var
  FontStyle : string;
begin
  if FontDialog1.Execute then
    begin
      memFont.Font.Name:= FontDialog1.Font.Name;
      memFont.Font.Style:= FontDialog1.Font.Style;
      memFont.Font.Size:= FontDialog1.Font.Size;
      memFont.Font.Color:= FontDialog1.Font.Color;
    end;
  memFont.Clear;
  memFont.Lines.Add('Font name: ' + FontDialog1.Font.Name);
  memFont.Lines.Add('Font size: ' + intToStr(FontDialog1.Font.Size));
  FontStyle := 'Font style: ';
  if fsBold in FontDialog1.Font.Style then
    FontStyle := FontStyle + ' bold ';
  if fsItalic in FontDialog1.Font.Style then
    FontStyle := FontStyle + ' italic ';
  if length(FontStyle) > 12 then
    memFont.Lines.Add(FontStyle);
end;

end.

uFontDialogDemo.lfm

object frmFont: TfrmFont
  Left = 453
  Height = 172
  Top = 110
  Width = 306
  Caption = 'Fonts'
  ClientHeight = 172
  ClientWidth = 306
  OnCreate = FormCreate
  LCLVersion = '0.9.30'
  object memFont: TMemo
    Left = 8
    Height = 128
    Top = 32
    Width = 288
    OnClick = memFontClick
    TabOrder = 0
  end
  object FontDialog1: TFontDialog
    MinFontSize = 0
    MaxFontSize = 0
    left = 56
    top = 65512
  end
end
        

FontDialogDemo.lpr

program FontDialogDemo;

uses
  Interfaces, Forms, uFontDialogDemo;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmFont, frmFont);
  Application.Run;
end.
Programming - a skill for life!

How to use message, file, colour, font and calendar dialogue boxes to interact with the user