如何在 Inno Setup 中更改进度条的颜色?

How do I change the color of my progress bar in Inno Setup?

我使用 TNewProgressBar 创建了一个进度条。
进度条默认颜色为绿色
我想将颜色更改为蓝色。

你不能。


进度条采用当前 Windows 主题的样式。在默认Windows主题中,进度条为绿色(或黄色或红色,如果进度条处于暂停或错误状态,请参见TNewProgressBar.State)。

您将不得不完全重新实现进度条的绘制或禁用整个安装程序的视觉主题。
参见 How to change the color of progressbar in C# .NET 3.5?

但是 Inno Setup API 不允许您重新实现绘图。而且您可能不想禁用视觉主题。


如果你真的需要蓝色,你可以考虑使用TBitmapImage.Bitmap.Canvas(使用类似.Rectangle的方法)自己实现进度条。

一个简单的例子:

var
  ProgressImage: TBitmapImage;

procedure InitializeWizard();
begin
  ProgressImage := TBitmapImage.Create(WizardForm);
  ProgressImage.Parent := WizardForm;
  ProgressImage.Left := ScaleX(10);
  ProgressImage.Top := WizardForm.ClientHeight - ScaleY(34);
  ProgressImage.Width := ScaleX(200);
  ProgressImage.Height := ScaleY(20);
  ProgressImage.BackColor := clWhite;
  ProgressImage.Bitmap.Width := ProgressImage.Width;
  ProgressImage.Bitmap.Height := ProgressImage.Height;
end;

procedure DrawProgress(Image: TBitmapImage; Progress: Integer);
var
  Canvas: TCanvas;
  Width: Integer;
begin
  Log(Format('Drawing progress %d', [Progress]));

  Canvas := Image.Bitmap.Canvas;

  Canvas.Pen.Style := psClear;

  Width := Image.Bitmap.Width * Progress / 100
  Log(Format('Bar size: %d x %d', [Width, Image.Bitmap.Height]));

  Canvas.Brush.Color := clHighlight;
  Canvas.Rectangle(1, 1, Width, Image.Bitmap.Height);

  Canvas.Brush.Color := clBtnFace;
  Canvas.Rectangle(Width - 1, 1, Image.Bitmap.Width, Image.Bitmap.Height);

  Canvas.Pen.Style := psSolid;
  Canvas.Pen.Mode := pmCopy;
  Canvas.Pen.Color := clBlack;
  Canvas.Brush.Style := bsClear;
  Canvas.Rectangle(1, 1, Image.Bitmap.Width, Image.Bitmap.Height);
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  Log(Format('CurPageChanged %d', [CurPageID]));
  DrawProgress(ProgressImage, (CurPageID * 100 / wpFinished));
end;

好久不见,我想分享一个改变Inno Setup安装进度条颜色的解决方案。我想这个解决方案可以应用于自定义进度条。我在谷歌搜索后制定了这个解决方案,但我没有更多参考资料。 附带说明:我没有卸载程序的解决方案。

  if CurPageID = wpInstalling then 
  begin
    Log('PAGE: wpInstalling ' + IntToStr(CurPageID));

    // Progress bar color cannot be changed from the application: request using Win DLL shall be done
    SendMessage(wizardform.progressgauge.Handle, PBM_SETBARCOLOR, 0, $DCA939); // Foreground color
    SendMessage(wizardform.progressgauge.Handle, PBM_SETBKCOLOR, 0, 2000);  // Background color
    //SendMessage(wizardform.progressgauge.Handle, PBM_SETMARQUEE, 1, 200);
  end;