Fabrizio
Fabrizio

Reputation: 8053

How to set the taskbar's progress indicator?

While performing long operations, some applications shows a progressbar behind the application icon, in the taskbar.

Screenshot of a taskbar with a progress bar in one of the buttons.

...

Screenshot of a taskbar with a progress bar in one of the buttons.

How can this progress indicator be set from a Delphi application?

Upvotes: 1

Views: 1992

Answers (2)

ALAIN WEBER
ALAIN WEBER

Reputation: 23

Here you have an example (link):

unit uMainTest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ObjectArray, ShlObj, ExtCtrls, ComCtrls, ActiveX, ComObj,
  ImgList, CommCtrl, PropSys;

type
  TfrmMain = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Button2: TButton;
    trackBar: TTrackBar;
    GroupBox5: TGroupBox;
    chkState1: TCheckBox;
    chkState2: TCheckBox;
    chkState3: TCheckBox;
    chkState4: TCheckBox;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btProgressBarDemoClick(Sender: TObject);
    procedure btProgressStateClick(Sender: TObject);
    procedure trackBarChange(Sender: TObject);
    procedure btConfigureTasksClick(Sender: TObject);
  private
    TaskBar: ITaskBarList3;
    msgTaskbarButtonCreated: cardinal;

  protected
    procedure WndProc(var Message: TMessage); override;
  public

  end;

var
  frmMain: TfrmMain;

implementation

uses Registry, ShellApi, uFileRegistration ;

{$R *.dfm}

procedure TfrmMain.btProgressBarDemoClick(Sender: TObject);
begin
  TaskBar.SetProgressValue(Handle, trackBar.Position, trackBar.Max);
end;

procedure TfrmMain.btProgressStateClick(Sender: TObject);
var
  Flags: integer;
begin
  Flags := TBPF_NOPROGRESS;
  if chkState1.Checked then
    Flags := Flags or chkState1.Tag;
  if chkState2.Checked then
    Flags := Flags or chkState2.Tag;
  if chkState3.Checked then
    Flags := Flags or chkState3.Tag;
  if chkState4.Checked then
    Flags := Flags or chkState4.Tag;
  TaskBar.SetProgressState(Handle, Flags);
end;

procedure TfrmMain.btConfigureTasksClick(Sender: TObject);
var
  JumpList: ICustomDestinationList;
  RemovedDestination: IObjectArray;
  TaskList: IObjectCollection;
  pcMaxSlots: cardinal;
  Link1: IShellLink;
begin
  JumpList := CreateComObject(CLSID_DestinationList) as ICustomDestinationList;
  OleCheck(JumpList.BeginList(pcMaxSlots, IID_IObjectArray,
    RemovedDestination));
  try

    // A présent, on prépare une nouvelle liste de tâches à ajouter dans la
    // JumpList.
    TaskList := CreateComObject(CLSID_EnumerableObjectCollection)
      as IObjectCollection;

    // Enfin, on définit la liste des tâches en appelant AddUserTasks.
    OleCheck(JumpList.AddUserTasks(TaskList));
  except
    // En cas d'erreur, il faut annuler la liste en cours de définition
    JumpList.AbortList;
    raise; // Puis on redéclenche l'exception.
  end;
  OleCheck(JumpList.CommitList);
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  msgTaskbarButtonCreated := RegisterWindowMessage('TaskbarButtonCreated');

  OleCheck(SetCurrentProcessExplicitAppUserModelID('Dvp.Delphi.DemoTaskbar.1'));
end;

procedure TfrmMain.trackBarChange(Sender: TObject);
begin
  TaskBar.SetProgressValue(Handle, trackBar.Position, trackBar.Max);
end;

procedure TfrmMain.WndProc(var Message: TMessage);
begin
  if Message.Msg = msgTaskbarButtonCreated then
    TaskBar := CreateComObject(CLSID_TaskbarList) as ITaskBarList3
  else
    inherited WndProc(Message);
end;

end.

Upvotes: 2

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109068

In modern Delphi versions, you have the TTaskbar component in the Win32 part of the component palette.

Drop that one on your form, set ProgressState (to Normal, say), ProgressMaxValue (to 100, say), and ProgressValue (to 50, say).

In my experience, this component is buggy -- or at least it has been in earlier versions. So be careful.

A safer alternative, of course, is to use the Win32 API directly. See the official documentation for ITaskbarList3.

Upvotes: 8

Related Questions