Determining if a Window Has a Taskbar Button
Windows uses heuristics to decide whether or not to give a taskbar button to a window, and sometimes there is a delay before it can decide, so doing this 100% accurately is going to be quite hard. Here's a rough start on the rules. There are modern style flags that make it easy to know, but when those styles are missing the taskbar is reduced to guessing.
First off, you will need both of the the window style flags.
LONG Style = GetWindowLong(hwnd, GWL_STYLE);LONG ExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
Now the rules, there are three rules that are certain.
- if
ExStyle & WS_EX_APPWINDOW
, then TASKBAR - if
ExStyle & WS_EX_TOOLWINDOW
, then NOT_TASKBAR - if
Style & WS_CHILD
then NOT_TASKBAR
The rest are guesses:
Style & WS_OVERLAPPED
suggests TASKBARStyle & WS_POPUP
suggests NOT_TASKBAR especially ifGetParent() != NULL
ExStyle & WS_EX_OVERLAPPEDWINDOW
suggests TASKBARExStyle & WS_EX_CLIENTEDGE
suggests NOT_TASKBARExStyle & WS_EX_DLGMODALFRAME
suggests NOT_TASKBAR
I'm sure that there are other rules for guessing, and in fact that the guessing rules have changed from version to version of Windows.
Toplevel window
WS_EX_APPWINDOW -> taskbar, no matter the other styles!
OWNER must be NULL (GetWindow(window, GW_OWNER))
no: WS_EX_NOACTIVATE or WS_EX_TOOLWINDOW:
order is important.
second question: in windows xp/vista it was possible to get into the process of the taskbar and get all window ID´s:
void EnumTasklistWindows(){ int b2 = 0; TBBUTTON tbButton; DWORD dwProcessId = 0, dwThreadId = 0; HWND hDesktop =::GetDesktopWindow(); HWND hTray =::FindWindowEx(hDesktop, 0, ("Shell_TrayWnd"), NULL); HWND hReBar =::FindWindowEx(hTray, 0, ("ReBarWindow32"), NULL); HWND hTask =::FindWindowEx(hReBar, 0, ("MSTaskSwWClass"), NULL); HWND hToolbar =::FindWindowEx(hTask, 0, ("ToolbarWindow32"), NULL); LRESULT count =::SendMessage(hToolbar, TB_BUTTONCOUNT, 0, 0); dwThreadId = GetWindowThreadProcessId(hToolbar, &dwProcessId); shared_ptr<void> hProcess (OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId), CloseHandle); if (NULL == hProcess.get()) { return; } memset(&tbButton, 0, sizeof(TBBUTTON)); for (int i = 0; i < count; i++) { memset(&tbButton, 0, sizeof(TBBUTTON)); shared_ptr<void> lpRemoteBuffer ( VirtualAllocEx(hProcess.get(), NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE), bind<BOOL>(VirtualFreeEx, hProcess.get(), _1, 0, MEM_RELEASE)); if (NULL == lpRemoteBuffer.get()) { return; } SendMessage(hToolbar, TB_GETBUTTON, i, (LPARAM) lpRemoteBuffer.get()); b2 = ReadProcessMemory(hProcess.get(), lpRemoteBuffer.get(), (LPVOID) & tbButton, sizeof(TBBUTTON), NULL); if (0 == b2) { continue; } BYTE localBuffer[0x1000]; BYTE *pLocalBuffer = localBuffer; DWORD_PTR ipLocalBuffer = (DWORD_PTR) pLocalBuffer; pLocalBuffer = localBuffer; ipLocalBuffer = (DWORD_PTR) pLocalBuffer; DWORD_PTR lpRemoteData = (DWORD_PTR) tbButton.dwData; ReadProcessMemory(hProcess.get(), (LPVOID) lpRemoteData, (LPVOID) ipLocalBuffer, sizeof(DWORD_PTR), NULL); HWND windowHandle; memcpy(&windowHandle, (void *) ipLocalBuffer, 4); if (windowHandle != NULL) { trace ("adding button: %x\n", windowHandle); } }}
this not possible with windows 7 anymore. so you need to loop over all toplevel windows.
This MSDN article has some good information about when and why the Shell decides to create a taskbar button for a window:
The Shell creates a button on the taskbar whenever an application creates a window that isn't owned. To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.