gpt4 book ai didi

c++ - 如何在写入图标 (.ico) 时为鼠标光标应用掩码

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:38:42 26 4
gpt4 key购买 nike

我使用 GetCursorInfo 来捕获光标,但在将光标另存为图标时,图标上出现了一些黑色矩形。

对于 Windows 默认光标很好,但我遇到这个问题的自定义光标很少 http://www.cursors-4u.com/

在链接中放置了一个示例光标图标 https://www.google.com/search?q=cursor+icon&rlz=1C1CHBD_enIN789IN789&source=lnms&tbm=isch&sa=X&ved=0ahUKEwix9aj_gq_iAhWCXCsKHcusD0oQ_AUIDigB&biw=1366&bih=657#imgrc=rOxlbRoBfnKs8M :

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;
}

我的日程是捕获光标并将光标保存到图标(.ico)文件或加载到缓冲区。

有没有其他方法可以将光标数据写入图标文件或缓冲区?

最佳答案

ICONINFO 结构包含两个成员,hbmMaskhbmColor,它们分别包含光标的 mask 和颜色位图(请参阅官方文档的 ICONINFOMSDN 页面)。

当您为默认光标调用 GetIconInfo() 时,ICONINFO 结构包含有效掩码和颜色位图。

There is probably a better way to render the cursor that the BitBlt() - BitBlt() - MakeTransparent() combination of method calls.

引用@Tarsier的做法,虽然是C#,但是思路是一样的。

链接:Capturing the Mouse cursor image

关于c++ - 如何在写入图标 (.ico) 时为鼠标光标应用掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56256645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com