How do I set medium weight font using TFontDialog?

Viewed 653

Some fonts have only medium and bold weight. Given the code below do I have to avoid TFontDialog all together? When choosing style medium from Dialog it returns weight 500 sets style BOLD. If I save this style and open TFontDialog back up its is now set to BOLD.

  FW_THIN = 100;
  {$EXTERNALSYM FW_EXTRALIGHT}
  FW_EXTRALIGHT = 200;
  {$EXTERNALSYM FW_LIGHT}
  FW_LIGHT = 300;
  {$EXTERNALSYM FW_NORMAL}
  FW_NORMAL = 400;
  {$EXTERNALSYM FW_MEDIUM}
  FW_MEDIUM = 500;
  {$EXTERNALSYM FW_SEMIBOLD}
  FW_SEMIBOLD = 600;
  {$EXTERNALSYM FW_BOLD}
  FW_BOLD = 700;
  {$EXTERNALSYM FW_EXTRABOLD}
  FW_EXTRABOLD = 800;
  {$EXTERNALSYM FW_HEAVY}
  FW_HEAVY = 900;

  {$EXTERNALSYM FW_REGULAR}
  FW_REGULAR = FW_NORMAL;

    procedure TFontDialog.UpdateFromLogFont(const LogFont: TLogFont);
     var
      Style: TFontStyles;
    begin
      with LogFont do
      begin
        Font.Name := LogFont.lfFaceName;
        Font.Height := LogFont.lfHeight;
        if FFontCharsetModified then
          Font.Charset := TFontCharset(LogFont.lfCharSet);
        Style := [];
        with LogFont do
        begin
          if lfWeight > FW_REGULAR then Include(Style, fsBold);
          if lfItalic <> 0 then Include(Style, fsItalic);
          if lfUnderline <> 0 then Include(Style, fsUnderline);
          if lfStrikeOut <> 0 then Include(Style, fsStrikeOut);
        end;
        Font.Style := Style;
      end;
    end;
2 Answers

You can retrieve the TLogFont font info using this code in the OnClose event:

var
 LogFont: TLogFont;
begin
 if Sender is TFontDialog then
  with Sender as TFontDialog do
   SendGetStructMessage(Handle, WM_CHOOSEFONT_GETLOGFONT, 0, LogFont);
end;

The following units are needed: Windows, Messages and CommDlg. I am still looking on how to set the TLogFont data to de dialog on show and how to use it with a TCanvas.

Related