Default
Google

How can I change the menu font?

Applies To: C++Builder 4 or higher
Category: Knowledge Base

 
 1.
  Start a new project using File | New Application.
 
 2.
  Add a Menu. Set OwnerDraw to true.
 
 3.
  Edit the OnShow event of your form:
 
void __fastcall TForm1::FormShow(TObject *Sender)
{
    for(int x = 0; x < ComponentCount; x++)< ComponentCount; x++)
    {
        TMenuItem *item = dynamic_cast<TMenuItem*>
            (Components[x]);
        if(item)
            item->OnDrawItem = DrawItem;
    }
}
 
 4.
  Open your header file:
 
private:
    void __fastcall DrawItem(TObject *Sender, TCanvas *ACanvas,
        const Windows::TRect &ARect, bool Selected);
 
 5.
  Insert in your source code
 
void __fastcall TForm1::DrawItem(TObject *Sender, TCanvas
    *ACanvas, const Windows::TRect &ARect, bool Selected)
{
    ACanvas->FillRect(ARect);
    ACanvas->Font->Name = "Arial";
    ACanvas->Font->Size = 9;
 
    AnsiString caption(dynamic_cast<TMenuItem*>(Sender)->
        Caption);
    for(int x = 1; x <= caption.Length(); x++)
    {
        if(caption[x] == '&')
        {
            ACanvas->Font->Style = TFontStyles() <<< fsUnderline;< fsUnderline;
            caption.Delete(x, 1);
            x--;
        }
        else
        {
            AnsiString part(caption);
            part.SetLength(x - 1);
            ACanvas->TextOut(ARect.Left + ACanvas->TextWidth(part),
                ARect.Top, caption[x]);
            ACanvas->Font->Style = TFontStyles() >> fsUnderline;
        }
    }
}
 
The first part of the code looks for all the menu items of your form and sets their OnDrawItem event to DrawItem. Once this event is handled, you can easily access the TCanvas object. The final problem is to consider that the & sign underlines the next letter.

C++Builder Developer's Network
Copyright © Yoto Yotov



Acquiring image from ProHosting Banner Exchange