我使用GetCursorInfo
捕获光标,但是将光标另存为图标时,图标上出现了一个黑色矩形。
对于Windows,默认光标很好,但我遇到此问题http://www.cursors-4u.com/
的自定义光标很少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 = CreateFileA(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 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;
}
//Capture cursor.
CURSORINFO getHCursor()
{
CURSORINFO cursorInfo;
cursorInfo.cbSize = sizeof(CURSORINFO);
if (GetCursorInfo(&cursorInfo) == 0)
{
MessageBox(NULL, _T("Exception : GetCursorInfo creation failed"),_T("message"),MB_OK|MB_SYSTEMMODAL);
cursorInfo.hCursor = NULL;
return cursorInfo;
}
return cursorInfo;
}
//Main Call
int _tmain(int argc, _TCHAR* argv[])
{
while (true)
{
CURSORINFO CursorInfo = getHCursor();
if (CursorInfo.hCursor == NULL)
{
::Sleep(MinSleep);
continue;
}
SaveIcon((HICON)CursorInfo.hCursor, "C:\\Users\\Desktop\\myicon.ico");
Sleep(MaxSleep);
}
return 0;
}
我的议程是捕获光标并将光标保存到icon..ico文件或加载到缓冲区。
还有其他方法可以将光标数据写入图标文件或缓冲区吗?
答案 0 :(得分:0)
ICONINFO
结构包含两个成员hbmMask
和hbmColor
,它们分别包含光标的蒙版和颜色位图(请参见MSDN页上的{ {1}}(用于官方文档)。
当您调用ICONINFO
作为默认光标时,GetIconInfo()
结构同时包含有效的掩码和颜色位图。
有一种更好的方式可以呈现光标
ICONINFO
-BitBlt()
-BitBlt()
组合方法 电话。
请参阅@Tarsier方法,尽管它是C#,但想法是相同的。