NO

Author Topic: Taskbar Progress  (Read 3994 times)

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Taskbar Progress
« on: January 27, 2024, 08:47:59 PM »
Hey everyone. I'm currently using the ITaskbarList3 interface to reporting progress and visual present it in the taskbar for the application.

https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-itaskbarlist3



Code: [Select]
#include <shobjidl.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "ole32.lib")

WM_INITDIALOG:
Code: [Select]
CoInitialize(NULL);
ITaskbarList3 *pTaskbarList;
CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, &IID_ITaskbarList3, (void**)&pTaskbarList);

HWND hDlg = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(DLG_MAIN), NULL, NULL);

// Set the taskbar progress value (example: 50%)
pTaskbarList->lpVtbl->SetProgressValue(pTaskbarList, hDlg, 50, 100);

// Perform other tasks...

// Clean up
pTaskbarList->lpVtbl->Release(pTaskbarList);
CoUninitialize();

Now it works fine, but it launches a new window to do it. How do I get progress on the main window dialog? It launches 2 windows, which is not desired.

Thank you!

EDIT:
Sorry, I solved the issue. It was a matter of calling inside APIENTRY wWinMain(...)

Code: [Select]
...
HWND hMainWnd = CreateDialog(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)MainDlgProc);

    if (hMainWnd == NULL)
    {
        // Handle error if the dialog creation fails
        MessageBox(NULL, L"Failed to create the main dialog", L"Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    // Initialize COM for the taskbar progress
    CoInitialize(NULL);
    ITaskbarList3 *pTaskbarList;

    // Create an instance of the taskbar list
    if (SUCCEEDED(CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, &IID_ITaskbarList3, (void**)&pTaskbarList)))
    {
        // Register the "TaskbarButtonCreated" message
        UINT uTaskbarCreatedMsg = RegisterWindowMessage(L"TaskbarButtonCreated");

        // Allow the main window to receive the "TaskbarButtonCreated" message
        ChangeWindowMessageFilterEx(hMainWnd, uTaskbarCreatedMsg, MSGFLT_ALLOW, NULL);

        // Set the taskbar progress value (example: 50%)
        pTaskbarList->lpVtbl->SetProgressValue(pTaskbarList, hMainWnd, 50, 100);

        // Perform other tasks...

        // Clean up
        pTaskbarList->lpVtbl->Release(pTaskbarList);
    }

    // Message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Uninitialize COM
    CoUninitialize();

    return msg.wParam;
}


------------------

UPDATE:

Better solution. This uses ITaskbarList3 interface.

Code: [Select]
typedef enum {
PS_None,
PS_Normal,
PS_Paused,
PS_Error,
PS_Indeterminate
} ProgressStates;

void UpdateTaskbarProgress(HWND hWnd, ProgressStates state, int progressValue) {
ITaskbarList3 *taskbar;

if (CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, &IID_ITaskbarList3, (void**)&taskbar) != S_OK) {
return;
}

taskbar->lpVtbl->HrInit(taskbar);
TBPFLAG flags = TBPF_NOPROGRESS;

switch (state) {
    case PS_Normal:       
        flags = TBPF_NORMAL;       
        break;
    case PS_Paused:       
        flags = TBPF_PAUSED;       
        break;
    case PS_Error:         
        flags = TBPF_ERROR;         
        break;
    case PS_Indeterminate:
        flags = TBPF_INDETERMINATE;
        break;
    }

taskbar->lpVtbl->SetProgressState(taskbar, hWnd, flags);

if (state == PS_Normal) {
        taskbar->lpVtbl->SetProgressValue(taskbar, hWnd, progressValue, 100);
    }

taskbar->lpVtbl->Release(taskbar);
}

Working as expected, with no issues ;)

Credits to:
https://github.com/CyberSys/qutim

Code is google cached (file does not exist anymore on the GIT link). Converted to C (above).

ITaskbarList4 interface appears to support adding buttons and things inside. I'll be testing this.

For example:
Code: [Select]
taskbar->lpVtbl->AddThumbBarButton(taskbar, hWnd, /* Button ID */ 1, THBF_ENABLED, NULL);
« Last Edit: January 27, 2024, 10:42:57 PM by WiiLF23 »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Taskbar Progress
« Reply #1 on: January 28, 2024, 01:32:57 AM »
Interesting stuff - compliments :)

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Re: Taskbar Progress
« Reply #2 on: January 28, 2024, 08:16:14 AM »
Much improved, with custom icons, tooltips, buttons and different states. Works a treat ;)