Recent

Author Topic: Task Icon Progress Bar under Windows  (Read 8923 times)

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Task Icon Progress Bar under Windows
« on: March 31, 2019, 12:57:55 pm »
Hello,

just want to share how to use the taskbar progress bar under Windows. I didn't find any Lazarus-specific description on the web, so it might be helpful for someone. Unfortunately its a Windows only solution.

See attached screenshots. The windows task icons offer the possibility to show a progress bar, this is useful for showing the progress of longer lasting actions, when the user doesn't have the application window in the foreground.

Regards~

--
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ShlObj, comobj, Win32Int, InterfaceBase, LCLIntf;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Button3: TButton;
  19.     Button4: TButton;
  20.     Label1: TLabel;
  21.     Label2: TLabel;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.     procedure Button3Click(Sender: TObject);
  25.     procedure Button4Click(Sender: TObject);
  26.     procedure FormCreate(Sender: TObject);
  27.   private
  28.       FTaskBarList: ITaskbarList3;
  29.       AppHandle: THandle;
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. IMPLEMENTATION
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.Button1Click(Sender: TObject);
  44. begin
  45.   FTaskBarList.SetProgressState(AppHandle, TBPF_NOPROGRESS);
  46.  
  47.   Application.Title:= 'project1';
  48. end;
  49.  
  50. procedure TForm1.Button2Click(Sender: TObject);
  51. begin
  52.   FTaskBarList.SetProgressState(AppHandle, TBPF_Normal);
  53.   FTaskBarList.SetProgressValue(AppHandle, 40, 100);
  54.  
  55.   Application.Title:= 'progress';
  56. end;
  57.  
  58. procedure TForm1.Button3Click(Sender: TObject);
  59. begin
  60.   FTaskBarList.SetProgressState(AppHandle, TBPF_Error);
  61.   FTaskBarList.SetProgressValue(AppHandle, 50, 100);
  62.  
  63.   Application.Title:= 'error';
  64. end;
  65.  
  66. procedure TForm1.Button4Click(Sender: TObject);
  67. begin
  68.   sleep(2000);
  69.   LCLIntf.SetForegroundWindow(AppHandle);
  70.  
  71. end;
  72.  
  73. procedure TForm1.FormCreate(Sender: TObject);
  74. begin
  75.   AppHandle := TWin32WidgetSet(WidgetSet).AppHandle;
  76.   FTaskBarList := CreateComObject(CLSID_TaskbarList) as ITaskbarList3;
  77.  
  78. end;
  79.  
  80. end.

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: Task Icon Progress Bar under Windows
« Reply #1 on: March 31, 2019, 01:14:09 pm »
I ever wondered how to do it.
Thanks for sharing it

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Task Icon Progress Bar under Windows
« Reply #2 on: March 31, 2019, 02:17:34 pm »
Yes, thank you for sharing that.  Very nice.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

AlexTP

  • Hero Member
  • *****
  • Posts: 2383
    • UVviewsoft
Re: Task Icon Progress Bar under Windows
« Reply #3 on: January 03, 2021, 09:32:51 pm »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Task Icon Progress Bar under Windows
« Reply #4 on: January 04, 2021, 10:43:17 am »
just want to share how to use the taskbar progress bar under Windows. I didn't find any Lazarus-specific description on the web, so it might be helpful for someone. Unfortunately its a Windows only solution.
Don't forget, that WinXP doesn't support ITaskbarList3.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   Base: IUnknown;
  4. begin
  5. //  AppHandle := TWin32WidgetSet(WidgetSet).AppHandle;
  6.   AppHandle := Application.Handle;
  7.   Base := CreateComObject(CLSID_TaskbarList);
  8.   if not Supports(Base, ITaskbarList3, FTaskBarList) then
  9.     FTaskBarList := nil;
  10. end;
and replace "FTaskBarList." with "if Assigned(FTaskBarList) then FTaskBarList.".

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: Task Icon Progress Bar under Windows
« Reply #5 on: January 06, 2021, 03:36:12 am »
@kupferstecher: thanks for sharing this example but, you forgot about the TBPF_PAUSED, which gives orange progress. :D
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

AlexTP

  • Hero Member
  • *****
  • Posts: 2383
    • UVviewsoft
Re: Task Icon Progress Bar under Windows
« Reply #6 on: January 06, 2021, 11:03:37 am »
1) TBPF_PAUSED in considered in my version
https://wiki.lazarus.freepascal.org/Win32TaskbarProgress

2) WinXP is considered as well, I have check for Win32MajorVersion>=6

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: Task Icon Progress Bar under Windows
« Reply #7 on: January 06, 2021, 11:34:06 am »
@Alextp: thank you, but your demo from the repository is somehow broken. If I set the progress value in the spin edit and then change the combobox value few times, the progress on the button does not match the spin edit value. Look at the attachment. This bug happen when the Animation style is set and then, one of the last three style. Besides, the demo produces memory leaks after closing the window. Use -gh option to see the problem.

Fun fact — the TBPF_INDETERMINATE style looks the same as TBPF_NORMAL with the progress state ignored. There is a green stripe at the top of the taskbar button, but without progress and any animation. I tested it myself and the animation does not work in my system. I have a broken Windows or it should not be animated? I don't know what to think about this “merque” style. 8)

My test application is added to the attachments — please, try it yourself.

Edit: problem solved — to be able to see progressbar animation in the indeterminate state, the Show animations in Windows option must be enabled in the system control panel. I had this option disabled, so the bar was not animated. So, the source code of my demo application is correct.
« Last Edit: January 06, 2021, 06:11:56 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Task Icon Progress Bar under Windows
« Reply #8 on: January 06, 2021, 07:56:42 pm »
Don't forget, that WinXP doesn't support ITaskbarList3.

Or Vista, too.

2) WinXP is considered as well, I have check for Win32MajorVersion>=6

ITaskbarList3 was added in Win7 (6.1), not in Vista (6.0).  So that check will not suffice, you would need this instead:

Code: [Select]
uses ..., SysUtils;
if (Win32MajorVersion > 6) or (Win32MajorVersion = 6 and Win32MinorVersion >= 1)

Or perhaps this:

Code: [Select]
uses ..., Win32Proc;
if WindowsVersion >= wv7

Of course, you really shouldn't be doing any OS version checking to begin with, but rather detecting the presence of ITaskbarList3 itself, like ASerge showed.
« Last Edit: January 06, 2021, 08:09:14 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018