具有透明度的嵌入式资源的glfwsetwindowicon
我正在尝试实现几乎每个应用程序的作用,这就是拥有一个可以在标题栏和任务栏中显示的嵌入式图标。
要求是:
- 图标必须支持透明度
- 该图标必须嵌入,而不是磁盘上的单独文件,
- 图标文件格式无关紧要;可以是ICO,BMP或其他有效的东西。
我尝试在paint.net中创建一个BMP,但是loadImage
一直返回null。我将其重新创建为32位PNG,然后使用 此工具将其转换为32位BMP,我的推理是我要获得loadImage
再次工作,这次是透明度。我正在使用loadImage
,因为显然loadbitmap
没有那么支持。
Resources.RC:
100 BITMAP "icons\\icon.bmp"
代码:
HBITMAP h_bmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(100), IMAGE_BITMAP, 0, 0, 0);
HDC dc = CreateCompatibleDC(NULL);
HBITMAP bmp_old = (HBITMAP)SelectObject(dc, h_bmp);
BITMAP bmp = {};
GetObject(h_bmp, sizeof(bmp), &bmp);
BITMAPINFO info;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = bmp.bmWidth;
info.bmiHeader.biHeight = -bmp.bmHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = bmp.bmBitsPixel;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = ((bmp.bmWidth * bmp.bmBitsPixel + 31) / 32) * 4 * bmp.bmHeight;
std::vector<unsigned char> pixels;
pixels.resize(info.bmiHeader.biSizeImage);
GetDIBits(dc, h_bmp, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
SelectObject(dc, bmp_old);
DeleteDC(dc);
GLFWimage image = { bmp.bmWidth, bmp.bmHeight, &pixels[0] };
glfwSetWindowIcon(window, 1, &image);
图像有两个问题;首先,它显示颠倒,因此对上方的高度否定。其次,RGB通道被翻转,我不知道如何修复它。
因此,是否有一种更简单的方法可以做到这一点,也许不必遭受BMP格式并需要第三方工具,如果没有,我该如何正确显示颜色?
I'm trying to achieve what almost every application does, and that's to have an embedded icon which I can show in the title bar and taskbar.
The requirements are:
- The icon must support transparency
- The icon must be embedded, not a separate file on the disk
- The icon file format doesn't matter; can be ico, bmp or anything else that works.
I tried to create a bmp in paint.net, but LoadImage
kept returning NULL. I recreated it as a 32-bit png, then used
this tool to convert it to a 32-bit bmp, my reasoning for which being to get LoadImage
working again, and this time with transparency. I'm using LoadImage
because apparently LoadBitmap
isn't so well-supported.
resources.rc:
100 BITMAP "icons\\icon.bmp"
code:
HBITMAP h_bmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(100), IMAGE_BITMAP, 0, 0, 0);
HDC dc = CreateCompatibleDC(NULL);
HBITMAP bmp_old = (HBITMAP)SelectObject(dc, h_bmp);
BITMAP bmp = {};
GetObject(h_bmp, sizeof(bmp), &bmp);
BITMAPINFO info;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = bmp.bmWidth;
info.bmiHeader.biHeight = -bmp.bmHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = bmp.bmBitsPixel;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = ((bmp.bmWidth * bmp.bmBitsPixel + 31) / 32) * 4 * bmp.bmHeight;
std::vector<unsigned char> pixels;
pixels.resize(info.bmiHeader.biSizeImage);
GetDIBits(dc, h_bmp, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
SelectObject(dc, bmp_old);
DeleteDC(dc);
GLFWimage image = { bmp.bmWidth, bmp.bmHeight, &pixels[0] };
glfwSetWindowIcon(window, 1, &image);
There were two issues with the image; firstly it was displaying upside-down, hence the negation of the height above. Secondly, the RGB channels are flipped and I have no idea how to fix it.
So, is there a simpler way to do this, perhaps without having to suffer bmp formats and needing 3rd-party tools, and if not, how can I get the colours displaying properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Windows(HWNDS)使用HICON。打孔是从.ICO文件或图标资源加载的图标的句柄。一个.ICO文件由一个或多种尺寸的一个或多个BMP和/或PNG文件组成。您应该使用真正的图标编辑器来创建图标。如果您 look org/gfie.php“ rel =“ nofollow noreferrer”>周围。
资源中有ICO文件后,将其加载
loadImage(hinst,...,image_icon,...,lr_shared)
并将其应用于setClasslongptr
或<代码> wm_seticon 。Windows (HWNDs) uses HICONs. A HICON is a handle to an icon loaded from a .ico file or an icon resource. A .ico file consists of one or more bmp and/or png files in various sizes. You should use a real icon editor to create your icon. You can find several free editors if you look around.
Once you have the ico file in your resources, load it with
LoadImage(hInst, ..., IMAGE_ICON, ..., LR_SHARED)
and apply it withSetClassLongPtr
orWM_SETICON
.根据 @user253751的评论解决;
比我预期的要简单得多!
Solved as per @user253751's comment;
Much simpler than I expected!