FontDialogOnApply (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example requires 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

TWinControl *myActiveControl;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  FontDialog1->Options << fdApplyButton;
  FontDialog1->Execute();
}

void __fastcall TForm1::FontDialog1Apply(TObject *Sender, HWND Wnd)
{
  if (myActiveControl->ClassNameIs("TEdit"))
    (dynamic_cast<TEdit *>(myActiveControl))->Font->Assign(FontDialog1->Font);
  else if (myActiveControl->ClassNameIs("TRichEdit"))
    (dynamic_cast<TRichEdit *>(myActiveControl))->SelAttributes->Assign(FontDialog1->Font);
  else
    MessageBeep(0);
}

void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
  myActiveControl = ActiveControl;
}

Uses

Personal tools
Assistance