Mega Code Archive

 
Categories / Delphi / VCL
 

How to display WYSIWYG fonts in a list box

Title: How to display WYSIWYG fonts in a list box Question: You can display the available fonts in a WYSIWYG fashion. Answer: Start a new project, add a TListBox, in the Forms OnCreate event code: procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items := Screen.Fonts ; end; //MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW the set the ListBoxs style property to lbOwnerDrawVariable and finally and the following code to the ListBoxs OnDrawItem and OnMeasureItem events procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer); var h : integer ; begin with Control as TListBox do begin Canvas.Font.Name := Items[Index] ; h := Canvas.TextHeight(Items[Index]) ; end ; Height := h ; end; //MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin with Control as TListBox do begin Canvas.Font.Name := Items[Index] ; Canvas.FillRect(Rect) ; Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]) ; end ; end; //MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW