Fonts

Setting ListBox font

Posted in Fonts

One of the ways is using WM_SETFONT message:

  SendMessage(
    ListBox1.Handle, 
    WM_SETFONT, 
    GetStockObject(System_Fixed_Font), 
    1);

Set rotated text

Posted in Fonts

Use TLOGFONT structure for creating new font. For rotating text you must change lfEscapement and lfOrientation fields of this structure.

procedure TForm1.Button1Click(Sender: TObject);
var
  MyLogFont: TLogFont;
  MyFont: HFont;
begin
  FillChar(MyLogFont, Sizeof(MyLogFont), 0);
  with MyLogFont do
  begin
    lfHeight:=0;
    lfWidth:=0;
    lfEscapement:=StrToInt(Edit1.Text);
    lfOrientation:=StrToInt(Edit1.Text);
    lfWeight:=FW_NORMAL;
    lfItalic:=1;
    lfUnderline:=1;
    lfStrikeOut:=0;
    lfCharSet:=DEFAULT_CHARSET;
    lfOutPrecision:=OUT_DEFAULT_PRECIS;
    lfClipPrecision:=CLIP_DEFAULT_PRECIS;
    lfQuality:=DEFAULT_QUALITY;
    lfPitchAndFamily:=1;
  end;
  MyFont:=CreateFontIndirect(MyLogFont);
  Form1.Canvas.Font.Handle:=MyFont;
  Form1.Canvas.TextOut(100, 100, 'Hello');
end;

Get font width/height in pixels

Posted in Fonts

This example gets Form.Font width and height. Use TextWidth and TextHeight methods of TCanvas type.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:='Width is '+
    IntToStr(Form1.Canvas.TextWidth(Edit1.Text))+
    ' pixels';
  Label2.Caption:='Height is '+
    IntToStr(Form1.Canvas.TextHeight(Edit1.Text))+
    ' pixels';;
end;

Set RichEdit font parameters

Posted in Fonts

SelAttributes.Style method of RichEditComponent. You may set bold, italic, underline, strikeout style and other parameters of font in RichEdit. And don't forget to return focus to RichEdit component.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if fsBold in RichEdit1.SelAttributes.Style then
    RichEdit1.SelAttributes.Style:=
      RichEdit1.SelAttributes.Style-[fsBold]
  else
    RichEdit1.SelAttributes.Style:=
      RichEdit1.SelAttributes.Style+[fsBold];
  RichEdit1.SetFocus;
end;

Detect all fonts

Posted in Fonts

All fonts you may get by using Fonts property of TScreen class.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Assign(Screen.Fonts);
end;