FontDialogOnApply (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses an edit control, a button, and a rich edit control on a form. When you press the button, a Font dialog appears. When you click the Apply (not OK) button in the Font dialog, the currently selected font is applied to the active control. Clicking the button sets the ActiveControl to the button. That is why you need to save the ActiveControl in a shared OnEnter procedure. In the rich edit, only the selected text converts.

Code

procedure TForm1.Button1Click(Sender: TObject);
begin
   FontDialog1.Options := FontDialog1.Options + [fdApplyButton];
   FontDialog1.Execute;
end;

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  myActiveControl := ActiveControl;
end;

procedure TForm1.FontDialog1Apply(Sender: TObject; Wnd: HWND);
begin
  if myActiveControl is TEdit then
    with myActiveControl as TEdit do
       Font.Assign(TFontDialog(Sender).Font)
  else if myActiveControl is TRichEdit then
    with myActiveControl as TRichEdit do
      SelAttributes.Assign(TFontDialog(Sender).Font)
  else
    Beep;
end;

Uses