C++ Builder

La Isla Bonita



Font Style
Font Style


Description

This example shows how to set and clear font styles. Form1 contains:
1) a Button (Set_StyleButton) to set the font style
2) A button (Clear_StyleButton) to clear the font style
3) A button (Show_StyleButton) to show the font style 4) A Rich Edit (RichEdit1)component to show the text on which the text style is applied.

FontStyle.cpp


#include <vcl.h>

#pragma hdrstop

#include "FontStyle.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  RichEdit1->DefAttributes->Color = clBlue;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Set_StyleButtonClick(TObject *Sender)
{
   RichEdit1->Font->Color = clRed;
   RichEdit1->Font->Style = TFontStyles()
                          << fsBold << fsItalic << fsUnderline
                          << fsStrikeOut;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Clear_StyleButtonClick(TObject *Sender)
{
   RichEdit1->Font->Color = clBlue;
   RichEdit1->Font->Style = TFontStyles()
                          >> fsBold >> fsItalic >> fsUnderline
                          >> fsStrikeOut;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Show_FontStyleButtonClick(TObject *Sender)
{
       String S1  = "Font Style (Method 1) : ";   //Used by Method 1
       String S2 = "Font Style (Method 2) : ";    //Used by Method 2

       //Method 1
       if ( RichEdit1->Font->Style.Contains( fsBold ) )
       {
          S1 += " Bold";
       }
       if ( RichEdit1->Font->Style.Contains( fsItalic ) )
       {
          S1 += " Italic";
       }
       if ( RichEdit1->Font->Style.Contains( fsUnderline ) )
       {
          S1 += " Underline";
       }
       if ( RichEdit1->Font->Style.Contains( fsStrikeOut ) )
       {
          S1 += " StrikeOut";
       }

      //Method 2: Another way would be
      for ( int i= 0; i<=3; ++ i )
      {
          if ( RichEdit1->Font->Style.Contains( (TFontStyle)i ) )
          {
             S2 += String((TFontStyle)i) + " ";
          }
      }

      ShowMessage( S1 + "\n" + S2 );
}

FontStyle.h

#ifndef FontStyleH
#define FontStyleH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Set_StyleButton;
        TButton *Show_FontStyleButton;
        TButton *Clear_StyleButton;
        TRichEdit *RichEdit1;
        void __fastcall Set_StyleButtonClick(TObject *Sender);
        void __fastcall Show_FontStyleButtonClick(TObject *Sender);
        void __fastcall Clear_StyleButtonClick(TObject *Sender);
       
private:	// User declarations
      TFontStyle Style;
public:		// User declarations
       __fastcall TForm1(TComponent* Owner);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.