| 1 |
"""Contains Windows Taskbar related functionality. |
| 2 |
|
| 3 |
Unfortunately the newly implemented `ctypes` interface is not working. From what I |
| 4 |
learned during my research is that the `comtypes` interface is able to support the |
| 5 |
`IUnkwown*` (or "early-binding") interfaces. That may not be the main reason. But, if |
| 6 |
it was working before let's not break it and keep the old code. |
| 7 |
|
| 8 |
Here is the code for future reference: |
| 9 |
|
| 10 |
.. code-block:: python |
| 11 |
import ctypes |
| 12 |
from ctypes import wintypes |
| 13 |
|
| 14 |
# Constants for taskbar progress states |
| 15 |
TBPF_NOPROGRESS = 0 |
| 16 |
TBPF_INDETERMINATE = 0x1 |
| 17 |
TBPF_NORMAL = 0x2 |
| 18 |
TBPF_ERROR = 0x4 |
| 19 |
TBPF_PAUSED = 0x8 |
| 20 |
|
| 21 |
|
| 22 |
# Define the ITaskbarList3 interface |
| 23 |
class ITaskbarList3(ctypes.Structure): |
| 24 |
_fields_ = [ |
| 25 |
("SetProgressValue", ctypes.c_void_p), |
| 26 |
("SetProgressState", ctypes.c_void_p), |
| 27 |
# Add other methods if needed |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
# Load the Shell32.dll and get the ITaskbarList3 interface |
| 32 |
shell32 = ctypes.windll.shell32 |
| 33 |
|
| 34 |
ITaskbarList3_ptr = ctypes.POINTER(ITaskbarList3) |
| 35 |
CoCreateInstance = ctypes.windll.ole32.CoCreateInstance |
| 36 |
CLSID_TaskbarList = ctypes.c_char_p(b"{56FDF344-FD6D-11d0-958A-006097C9A090}") |
| 37 |
IID_ITaskbarList3 = ctypes.c_char_p(b"{EA1AFB91-9E28-4B86-90E9-9E9F8A5EEFAF}") |
| 38 |
|
| 39 |
|
| 40 |
class Taskbar: |
| 41 |
def __init__(self, frame, maxv=100): |
| 42 |
self.frame = frame |
| 43 |
self.maxv = maxv |
| 44 |
self.taskbar = self._create_taskbar_instance() |
| 45 |
|
| 46 |
def _create_taskbar_instance(self): |
| 47 |
taskbar = ITaskbarList3_ptr() |
| 48 |
hr = CoCreateInstance( |
| 49 |
ctypes.byref(ctypes.c_void_p(CLSID_TaskbarList)), |
| 50 |
None, |
| 51 |
1, # CLSCTX_INPROC_SERVER |
| 52 |
ctypes.byref(IID_ITaskbarList3), |
| 53 |
ctypes.byref(taskbar), |
| 54 |
) |
| 55 |
if hr != 0: |
| 56 |
raise ctypes.WinError(hr) |
| 57 |
return taskbar |
| 58 |
|
| 59 |
def set_progress_value(self, value): |
| 60 |
if self.frame: |
| 61 |
hwnd = self.frame.GetHandle() |
| 62 |
self.taskbar.contents.SetProgressValue(hwnd, value, self.maxv) |
| 63 |
|
| 64 |
def set_progress_state(self, state): |
| 65 |
if self.frame: |
| 66 |
hwnd = self.frame.GetHandle() |
| 67 |
self.taskbar.contents.SetProgressState(hwnd, state) |
| 68 |
|
| 69 |
""" |
| 70 |
|
| 71 |
# Third Party Imports |
| 72 |
import wx |
| 73 |
from comtypes import client |
| 74 |
from comtypes.gen import TaskbarLib |
| 75 |
|
| 76 |
TBPF_NOPROGRESS = 0 |
| 77 |
TBPF_INDETERMINATE = 0x1 |
| 78 |
TBPF_NORMAL = 0x2 |
| 79 |
TBPF_ERROR = 0x4 |
| 80 |
TBPF_PAUSED = 0x8 |
| 81 |
|
| 82 |
taskbar = client.CreateObject( |
| 83 |
"{56FDF344-FD6D-11d0-958A-006097C9A090}", interface=TaskbarLib.ITaskbarList3 |
| 84 |
) |
| 85 |
taskbar.HrInit() |
| 86 |
|
| 87 |
|
| 88 |
class Taskbar: |
| 89 |
"""Taskbar progress bar interface. |
| 90 |
|
| 91 |
Args: |
| 92 |
frame (wx.Frame): The frame to associate with the taskbar progress. |
| 93 |
maxv (int): The maximum value for the progress bar, default is 100. |
| 94 |
""" |
| 95 |
|
| 96 |
def __init__(self, frame: wx.Frame, maxv: int = 100) -> None: |
| 97 |
self.frame = frame |
| 98 |
self.maxv = maxv |
| 99 |
|
| 100 |
def set_progress_value(self, value: int) -> None: |
| 101 |
"""Set the taskbar progress value. |
| 102 |
|
| 103 |
Args: |
| 104 |
value (int): The progress value, should be between 0 and maxv. |
| 105 |
""" |
| 106 |
if self.frame: |
| 107 |
taskbar.SetProgressValue(self.frame.GetHandle(), value, self.maxv) |
| 108 |
|
| 109 |
def set_progress_state(self, state: int) -> None: |
| 110 |
"""Set the taskbar progress state. |
| 111 |
|
| 112 |
Args: |
| 113 |
state (int): The progress state, one of TBPF_* constants. |
| 114 |
""" |
| 115 |
if self.frame: |
| 116 |
taskbar.SetProgressState(self.frame.GetHandle(), state) |