2011-03-03 79 views
4

如何将一些值添加到TFontDialog中的颜色框中? 或者请告诉我有关可以使用自定义颜色选择字体的组件? 我使用Delphi 7.在Delphi 7中将自定义颜色添加到TfontDialog中

谢谢。

我发现了一些方法......但是,如何在colorInbox = itemIndex =时更改TColorDialog?

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; 
+1

编写您自己的字体对话框。这不是很难吗? – 2011-03-04 00:56:10

+0

+1 to @Warren P.现在我不知道OP正在写什么类型的应用程序,但是如果字体选择是应用程序中的一项重要操作,那么实现一个自定义字体选择对话框是非常合理的,是非常容易和有趣的(我想 - 例如看看[我的自定义颜色选择器](http://privat.rejbrand.se/rejbrandcolourselector1.png))。 – 2011-03-04 20:54:15

+0

是的,创建自己的对话并不困难,但很长一段时间... Windows提供了良好的对话框,但它不包含“CustomColors”...顺便说一句,我找到了一些解决方案,但Andreas Rejbrand的答案可能会更好。 – 2011-03-05 11:49:33

回答

6

我想这样的作品:

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; 

但注意,大卫在下面的意见正确地指出,如果对话框应该改变(显著足够)在Windows的未来版本的代码可能会失败。这可能会或可能不会成为OP的最佳选择。

+3

这是非常调皮,你不知道! – 2011-03-03 15:19:04

+0

@David:恐怕我无法确定这是否意味着对这种方法的笑话或实际担忧。我没有搞乱信息处理,是吗? – 2011-03-03 15:35:30

+2

那么,这使得关于作为实现细节的对话框上的控件的假设成为可能。我希望你了解了细节,但我认为这种方法很脆弱。 – 2011-03-03 15:37:56

2

TFontDialog是系统的通用对话框,ChooseFont周围的包装。通用对话框不能轻松定制其颜色下拉菜单。

+0

我找到了一些方法:)但它不够... – 2011-03-03 14:22:33

相关问题