如何以编程方式将应用程序图标固定到Windows 8中的metro启动屏幕

时间:2013-04-26 09:32:07

标签: c++ windows-8 windows-installer startmenu

如何以编程方式(c ++)将应用程序图标固定到win8的metro开始屏幕?我知道如何手动完成。我也知道,一旦我启动该应用程序,它将自动添加。

1 个答案:

答案 0 :(得分:1)

我找到了这个解决方案here

BOOL PinToStart( LPCWSTR szFilePath )
{
BOOL bSuccess = FALSE;

// break into file name and path
WCHAR lpszDirectoryName[ MAX_PATH ] = { 0 };
LPCWSTR lpszFileName = ::PathFindFileName( szFilePath );

wcscpy_s( lpszDirectoryName, szFilePath );
::PathRemoveFileSpec( lpszDirectoryName );

// load shell32.dll
HMODULE hShell32 = LoadLibrary( L"SHELL32" );
if( hShell32 != NULL )
{
    // get the localized translation of 'Pin to Start' verb
    WCHAR szPinToStartLocalized[ MAX_PATH ] = { 0 };
    int nPinToStartLocalizedLength = LoadString( (HINSTANCE)hShell32, 51201, szPinToStartLocalized, MAX_PATH );

    if(  nPinToStartLocalizedLength > 0 )
    {
        // create the shell object
        IShellDispatch *pShellDispatch = NULL;
        HRESULT hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&pShellDispatch);
        if( SUCCEEDED( hr ) )
        {
            Folder *pFolder = NULL;
            variant_t vaDirectory( lpszDirectoryName );

            // get the namespace
            if( SUCCEEDED( pShellDispatch->NameSpace( vaDirectory, &pFolder ) ) )
            {
                FolderItem *pItem = NULL;
                bstr_t vaFileName( lpszFileName );

                // parse the name
                if( SUCCEEDED( pFolder->ParseName( vaFileName, &pItem ) ) )
                {
                    FolderItemVerbs* pVerbs = NULL;

                    // get the verbs
                    if( SUCCEEDED( pItem->Verbs(&pVerbs) ) )
                    {
                        long nCount = 0;

                        if( SUCCEEDED ( pVerbs->get_Count( &nCount ) ) )
                        {
                            variant_t vaIndex;
                            vaIndex.vt = VT_I4;

                            // iterate through verbs
                            for( vaIndex.lVal = 0; vaIndex.lVal<nCount; vaIndex.lVal++ )
                            {
                                FolderItemVerb* pVerb = NULL;

                                if( SUCCEEDED( pVerbs->Item( vaIndex, &pVerb ) ) )
                                {
                                    BSTR bstrVerbName = NULL;

                                    // check for 'Pin to Start' verb
                                    if( SUCCEEDED( pVerb->get_Name( &bstrVerbName ) ) )
                                    {
                                        if( 0 == wcscmp( bstrVerbName, szPinToStartLocalized ) )
                                        {
                                            bSuccess = SUCCEEDED( pVerb->DoIt() );
                                            vaIndex.lVal = nCount; // break for
                                        }

                                        ::SysFreeString( bstrVerbName );
                                    }
                                    pVerb->Release();
                                } // if
                            } // for
                        }
                        pVerbs->Release();
                    }
                    pItem->Release();
                }
                pFolder->Release();
            }
            pShellDispatch->Release();
        }
    }

    ::FreeLibrary( hShell32 );
}

return bSuccess;
}

希望它对你有帮助