sazr
sazr

Reputation: 25928

Obtain Handle to Task Bar

Is there a WinAPI function to retrieve a handle to the Task Bar?

The purpose is to determine the Task Bar docking setting (ABE_LEFT, ABE_RIGHT, ABE_BOTTOM, ABE_TOP). The function SHAppBarMessage requires the taskbar handle to retrieve the docking information. Unless there is another way to determine the task bar docking setting without needing the handle?

I'm aware of this method which works ok but I am not sure it works on all Windows versions:

HWND taskBar = FindWindow("Shell_TrayWnd", NULL);

Upvotes: 1

Views: 2605

Answers (1)

IInspectable
IInspectable

Reputation: 51395

That appears to be a documentation bug. You don't need to provide a window handle in the APPBARDATA structure for the ABM_GETTASKBARPOS when calling SHAppBarMessage1).

The following code properly returns the location of the taskbar (tested on Windows 10 x64):

#include <shellapi.h>
#pragma comment(lib, "Shell32.lib")
#include <stdexcept>

RECT GetTaskbarPos() {
    APPBARDATA abd = { 0 };
    abd.cbSize = sizeof( abd );
    if ( !::SHAppBarMessage( ABM_GETTASKBARPOS, &abd ) ) {
        throw std::runtime_error( "SHAppBarMessage failed." );
    }
    return abd.rc;
}

Update: The question was really asking for the docking enumeration value. That is returned as well:

#include <shellapi.h>
#pragma comment(lib, "Shell32.lib")
#include <stdexcept>

UINT GetTaskbarDockingEdge() {
    APPBARDATA abd = { 0 };
    abd.cbSize = sizeof( abd );
    if ( !::SHAppBarMessage( ABM_GETTASKBARPOS, &abd ) ) {
        throw std::runtime_error( "SHAppBarMessage failed." );
    }
    return abd.uEdge;
}


1) It would be awkward if you needed the well hidden window handle of the taskbar to send this message. If you had the window handle already, you could simply call GetWindowRect instead.

Upvotes: 1

Related Questions