7,540
社区成员
发帖
与我相关
我的任务
分享
// It's very weird that ANI cursors could not correctly load from embedded resources.
// For my tests, some ANI cursors can load via resources, but others do not.
// So we have to use temporary files instead.
HCURSOR LoadAniCursor(DWORD dwResID)
{
// Create temp file
TCHAR szTempPath[_MAX_PATH];
TCHAR szTempFile[_MAX_PATH];
if(!GetTempPath(_MAX_PATH, szTempPath)) return false;
if(!GetTempFileName(szTempPath, _T("ani"), 0, szTempFile)) return false;
//MessageBox(szTempFile);
HANDLE hFile = CreateFile(szTempFile, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile == INVALID_HANDLE_VALUE) goto failure;
// Load resource
HRSRC hrsrc = FindResource(0, MAKEINTRESOURCE(dwResID), RT_ANICURSOR);
if(hrsrc == 0) goto failure;
HGLOBAL hrc = LoadResource(0, hrsrc);
if(hrc == 0) goto failure;
LPBYTE pRcData = (LPBYTE)LockResource(hrc);
if(pRcData == 0) goto failure;
DWORD dwRcSize = SizeofResource(0, hrsrc);
if(dwRcSize == 0) goto failure;
DWORD cb = 0;
if(!WriteFile(hFile, pRcData, dwRcSize, &cb, 0)) goto failure;
CloseHandle(hFile);
HCURSOR hcur = (HCURSOR)LoadImage(0, szTempFile, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE | LR_SHARED);
//DWORD dw = GetLastError();
DeleteFile(szTempFile);
return hcur;
failure:
if(hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
DeleteFile(szTempFile);
return 0;
}