Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom colors to TfontDialog in Delphi 7

Tags:

winapi

delphi

How i can add some values to color Box in the TFontDialog? Or please tell me about components that can select font with custom color? I use Delphi 7.

Thanks.

I found some way... But how i can show TColorDialog when color box changed on itemIndex = 0?

 procedure TForm1.FontDialog1Show(Sender: TObject);
const
 IDCOLORCMB = $473;
 SMyColorName: PChar = 'clMoneyGreen';
 CMyColor: TColor = clMoneyGreen;
begin
 SendDlgItemMessage(FontDialog1.Handle, IDCOLORCMB, CB_INSERTSTRING, 0,
 Integer(SMyColorName));
 SendDlgItemMessage(FontDialog1.Handle, IDCOLORCMB, CB_SETITEMDATA, 0,
   ColorToRGB(CMyColor));
end;
like image 502
Dr. Serg Avatar asked Mar 03 '11 13:03

Dr. Serg


2 Answers

I think this works:

interface

TFontDialog = class(Dialogs.TFontDialog)
const
  IDCOLORCMB = $473;
protected
  procedure WndProc(var Message: TMessage); override;
  procedure DoShow; override;
end;

...

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  FontDialog1.Execute();
end;

{ TFontDialog }

procedure TFontDialog.DoShow;
const
  SMyColorName: PChar = 'Custom...';
  CMyColor: TColor = $0033ccff;
begin
  SendDlgItemMessage(Handle, IDCOLORCMB, CB_INSERTSTRING, 0, Integer(SMyColorName));
  SendDlgItemMessage(Handle, IDCOLORCMB, CB_SETITEMDATA, 0, ColorToRGB(CMyColor));
end;

procedure TFontDialog.WndProc(var Message: TMessage);
begin
  inherited;
  with Message do
    if (Msg = WM_COMMAND) and (WParamHi = CBN_SELENDOK) and (WParamLo = IDCOLORCMB) and (SendDlgItemMessage(Handle, IDCOLORCMB, CB_GETCURSEL, 0, 0) = 0) then
      with TColorDialog.Create(Self) do
        try
          Color := TColor(SendDlgItemMessage(Self.Handle, IDCOLORCMB, CB_GETITEMDATA, 0, 0));
          Options := [cdFullOpen];
          if Execute(Self.Handle) then
            SendDlgItemMessage(Self.Handle, IDCOLORCMB, CB_SETITEMDATA, 0, ColorToRGB(Color));
        finally
          Free;
        end;
end;

But notice, as David correctly argues in the comments below, that this code might fail if the dialog box should change (significantly enough) in a future version of Windows. That might or might not be a showstopper for the OP.

like image 68
Andreas Rejbrand Avatar answered Sep 30 '22 04:09

Andreas Rejbrand


TFontDialog is a wrapper around the system's common dialog, ChooseFont. The common dialog does not offer easy customisation of its colour drop-down.

like image 21
David Heffernan Avatar answered Sep 30 '22 03:09

David Heffernan