Thursday, December 22, 2016

[HowTo] Allow host app to be pinned to the taskbar


#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
#include <propvarutil.h>

HRESULT PropertyStoreSetStringValue(IPropertyStore* propertyStore, REFPROPERTYKEY pkey, PCWSTR value)
{
    PROPVARIANT propVariant;
    HRESULT hr = InitPropVariantFromString(value, &propVariant);
    if (SUCCEEDED(hr))
    {
        hr = propertyStore->SetValue(pkey, propVariant);
        PropVariantClear(&propVariant);
    }
    return hr;
}

HRESULT MakeWindowPinnable(HWND hwnd, PCWSTR userModelId, PCWSTR relaunchCommand, PCWSTR relaunchDisplayName, PCWSTR relaunchIcon = NULL)
{
    IPropertyStore* propertyStore;
    HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&propertyStore));
    if (SUCCEEDED(hr))
    {
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_ID, userModelId);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchCommand, relaunchCommand);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchDisplayNameResource, relaunchDisplayName);
        
        if (relaunchIcon != NULL)
        {
            PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchIconResource, relaunchIcon);
        }

        propertyStore->Release();
    }
    return hr;
}

char title[256];
GetWindowText(hwnd, title, sizeof(title));

MakeWindowPinnable(hwnd, title, GetCommandLine(), title);

No comments:

Post a Comment