Delphi - 颜色对话框 select 黑色作为颜色

Delphi - Colordialog select Black as color

我是 Delphi 的新人,我需要一些帮助。

我有程序,一个背景颜色转换器,带有 TColorDialog。这在其他方面完美无缺,但是当我关闭 ColorDialog 而不从中选择颜色时,它会出现问题。然后背景变为黑色,它还在 .ini 文件中将黑色保存为颜色“0”。

我的代码:

procedure TEinstellungenF.BtnBackgroundClick(Sender: TObject);
var filename:String;
ini:TIniFile;
begin       // Speichert die im ColorDialog ausgewählten Background Farben in der Ini
  filename := ExtractFilePath(ParamStr(0)) + 'Einstellungen.ini';
  ini := TIniFile.Create(filename);
  ColorDialog1.Execute();
  try
    ini.WriteInteger('Farben','Hintergrundfarbe', ColorDialog1.Color);
  finally
    ini.Free;
  end;

  FrmMain.Color := TColor(Ini.ReadInteger('Farben','Hintergrundfarbe',ColorDialog1.Color));
  Color := TColor(Ini.ReadInteger('Farben','Hintergrundfarbe',ColorDialog1.Color));
end;``` 

检查 ColorDialog1.Execute 的返回值并采取相应措施。

来自文档:

Execute opens the color-selection dialog, returning true when the user selects a color and clicks OK, or false when the user cancels.