- 1 year ago
wxWidgets windows 10 taskbar progress bar for app
To add a taskbar progress bar for your wxWidgets application on Windows 10, you can use the native Windows API functions provided by the wxTaskBarIcon
class. Here's a step-by-step guide to achieving this:
Include the necessary headers:
Include the necessary headers for using wxWidgets and the native Windows API functions.cpp#include <wx/wx.h>
#include <wx/taskbar.h>
#include <Windows.h>
Create a custom
wxTaskBarIcon
class:
Derive a custom class fromwxTaskBarIcon
to handle the taskbar progress bar.cppclass MyTaskBarIcon : public wxTaskBarIcon
{
public:
virtual wxMenu* CreatePopupMenu();
void SetProgress(double progress);
};
Implement the
CreatePopupMenu
function:
Define the function to create the context menu when the user right-clicks the taskbar icon.cppwxMenu* MyTaskBarIcon::CreatePopupMenu()
{
wxMenu* menu = new wxMenu;
menu->Append(wxID_EXIT, _("E&xit"));
return menu;
}
Implement the
SetProgress
function:
Define the function to set the progress value for the taskbar progress bar.cppvoid MyTaskBarIcon::SetProgress(double progress)
{
HWND hwnd = GetHWND();
CoInitialize(NULL); // Initialize COM for ITaskbarList3
ITaskbarList3* pTaskbarList;
CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (LPVOID*)&pTaskbarList);
if (pTaskbarList)
{
pTaskbarList->SetProgressValue(hwnd, static_cast<ULONGLONG>(progress * 100), 100);
pTaskbarList->Release();
}
CoUninitialize();
}
Implement the main application class:
Create your main application class that derives fromwxApp
.cppclass MyApp : public wxApp
{
public:
virtual bool OnInit();
};
Implement the
OnInit
function:
Define theOnInit
function to initialize the application and create the taskbar icon.cppbool MyApp::OnInit()
{
if (!wxApp::OnInit())
return false;
MyTaskBarIcon* taskBarIcon = new MyTaskBarIcon;
taskBarIcon->SetIcon(wxICON(wxLOGO), "My App");
return true;
}
Set the progress in your application:
Wherever you want to set the progress of the taskbar progress bar, call theSetProgress
function of yourMyTaskBarIcon
object.cppMyTaskBarIcon* taskBarIcon = new MyTaskBarIcon;
taskBarIcon->SetIcon(wxICON(wxLOGO), "My App");
// Set the progress to 0.5 (50%)
taskBarIcon->SetProgress(0.5);
Start the application:
Start your wxWidgets application using thewxIMPLEMENT_APP
macro.cppwxIMPLEMENT_APP(MyApp);
This code will create a simple wxWidgets application with a taskbar icon. When you set the progress using the SetProgress
function, it will update the taskbar progress bar accordingly.
Remember that this progress bar will be visible only on Windows 10 or later versions of Windows, as it uses the ITaskbarList3
interface, which is available on Windows 7 and later versions. On older versions of Windows, the taskbar progress bar won't be shown, but the application will still work without errors.