更换快捷方式图标

//更改桌面图标
    wchar_t path[1024];
    SHGetSpecialFolderPath(0, path, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
    wstring DesktopPath(path);
    DesktopPath.append(L"\\xxxx.lnk");

    //MessageBox(0, DesktopPath.c_str(),NULL,NULL);

    wstring wsIconPath = ui::PathUtil::GetCurrentModuleDir();
    wsIconPath += L".ico";

    if (!PathFileExists(DesktopPath.c_str()))
    {
        DesktopPath.clear();
        wmemset(path,0,1024);
        SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, 0);
        DesktopPath = path;
        DesktopPath.append(L"\\xxx.lnk");
        //MessageBox(0, DesktopPath.c_str(), NULL, NULL);
    }
    
    if (PathFileExists(wsIconPath.c_str()))
    {
        ChangeLinkIcon(DesktopPath, wsIconPath);
    }

 

bool ChangeLinkIcon(const wstring &strLnkName, const wstring &strIconPath)
{
    if (strLnkName.empty() || strIconPath.empty())
    {
        return false;
    }

    HRESULT hres;
    IShellLink *psl = NULL;
    IPersistFile *pPf = NULL;
    int id;
    LPITEMIDLIST pidl;
    bool bRet = false;

    do
    {
        hres = CoInitialize(NULL);
        if (FAILED(hres))
        {
            break;
        }

        hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
        if (FAILED(hres))
        {
            break;
        }

        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
        if (FAILED(hres))
        {
            break;
        }

        hres = pPf->Load(strLnkName.c_str(), STGM_READWRITE);
        if (FAILED(hres))
        {
            break;
        }

        hres = psl->SetIconLocation(strIconPath.c_str(), 0);
        if (FAILED(hres))
        {
            break;
        }

        pPf->Save(strLnkName.c_str(), TRUE);
        if (FAILED(hres))
        {
            break;
        }

        bRet = true;

    } while (0);

    if (pPf != NULL)
    {
        pPf->Release();
    }

    if (psl != NULL)
    {
        psl->Release();
    }

    CoUninitialize();

    return bRet;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值