获取图标(失真)

//在客户端上一级目录创建一个文件夹
//inloadName 创建的文件夹名称
//outFileload  文件夹的路径
int SoftInfosetDownloadLoad(std::string inloadName, std::string& outFileload)
{
	//用于存储模块路径
	CHAR szMoudlePath[MAX_PATH];
	//使用NULL参数,获取本模块路径
	if (!GetModuleFileNameA(NULL, szMoudlePath, MAX_PATH))
	{
		return -1;
	}
	std::string folderPath = +szMoudlePath;
	//查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)
	int last = folderPath.find_last_of("\\");
	int secLast = (folderPath.substr(0, last)).find_last_of("\\");
	std::string finalFolderPath = folderPath.substr(0, secLast);

	finalFolderPath += "\\";
	finalFolderPath += inloadName;



	if ((GetFileAttributesA(finalFolderPath.c_str()) == FILE_ATTRIBUTE_DIRECTORY))
	{
		outFileload = finalFolderPath;
		return 0;
	}
	else {
		bool flag = CreateDirectory(finalFolderPath.c_str(), NULL);
		if (!flag)
		{
			return -1;
		}
	}

	outFileload = finalFolderPath;

	return 0;
}


HRESULT SaveIcon(HICON hIcon, const char* path) {
	// Create the IPicture intrface
	PICTDESC desc = { sizeof(PICTDESC) };
	desc.picType = PICTYPE_ICON;
	desc.icon.hicon = hIcon;
	IPicture* pPicture = 0;
	HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void**)&pPicture);
	if (FAILED(hr)) return hr;

	// Create a stream and save the image
	IStream* pStream = 0;
	CreateStreamOnHGlobal(0, TRUE, &pStream);
	LONG cbSize = 0;
	hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);

	// Write the stream content to the file
	if (!FAILED(hr)) {
		HGLOBAL hBuf = 0;
		GetHGlobalFromStream(pStream, &hBuf);
		void* buffer = GlobalLock(hBuf);
		HANDLE hFile = CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, 0);
		if (!hFile) hr = HRESULT_FROM_WIN32(GetLastError());
		else {
			DWORD written = 0;
			WriteFile(hFile, buffer, cbSize, &written, 0);
			CloseHandle(hFile);
		}
		GlobalUnlock(buffer);
	}
	//Cleanup
	pStream->Release();
	pPicture->Release();
	return hr;

}

int GetLogo(std::string inExePath, std::string inlogoName, std::string inlogoFilename, std::string& outLogoPath)
{
	LPCSTR testIcon = inExePath.c_str();
	HICON hIcon = NULL;
	SHFILEINFOA fileInfo;
	//将获取的图标信息传到 fileinfo
	DWORD_PTR dwRet = SHGetFileInfoA(testIcon, 0, &fileInfo, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
	hIcon = fileInfo.hIcon;
	if (!hIcon) {
		return -1;
	}
	else {
		std::string outLogoFile;
		SoftInfosetDownloadLoad(inlogoFilename, outLogoFile);

		//添加名字
		outLogoFile += "\\";
		outLogoFile += inlogoName;
		HRESULT hr = SaveIcon(hIcon, outLogoFile.c_str());
		outLogoPath = outLogoFile.c_str();
	}

	::DestroyIcon(hIcon);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值