使用资源中的PNG图标显示Win32弹出菜单
Show Win32 popup menu with PNG icons from resources
我已经很久没有处理Win32菜单了。我需要在Win32上下文弹出菜单中添加一些PNG图标。当然,我想在这个过程中保留PNG的透明度和所有的每像素alpha。这可能吗?
我在考虑使用SetMenuItemBitmaps
。这是要走的路吗?
我将我的PNG导入为"PNG"资源,但我似乎既不能用LoadBitmap
也不能用LoadImage
加载它们。我发现了一些关于使用Gdi+
的建议,但显然我不会绘制菜单——系统会的。
似乎有一种方法可以从Gdi+Bitmap
中获得HBITMAP
,但在这个过程中,似乎所有的alpha都丢失了。AFAIK,HBITMAP
可以愉快地托管阿尔法信息。
您需要GDI+来加载PNG。然后,您需要创建一个正确大小的32位alpha位图,在位图上创建一个Graphics,并使用DrawImage将PNG复制到位图中。这将为您提供一个带有alpha通道的位图。
类似这样的东西:
Image* pimgSrc = Image::FromFile("MyIcon.png"L, FALSE);
Bitmap* pbmpImage = new Bitmap(
iWidth, iHeight,
PixelFormat32bppARGB
);
Graphics* pgraphics = Graphics::FromImage(bmpImage))
{
// This draws the PNG onto the bitmap, scaling it if necessary.
// You may want to set the scaling quality
graphics->DrawImage(
imageSrc,
Rectangle(0,0, bmpImage.Width, bmpImage.Height),
Rectangle(0,0, imgSrc.Width, imgSrc.Height),
GraphicsUnitPixel
);
}
// You can now get the HBITMAP from the Bitmap object and use it.
// Don't forget to delete the graphics, image and bitmap when done.
也许您可以使用icon?
以下是我使用图标而不是PNG:的原因
- Win32 API有很好的支持图标,而且绘制图标相对容易得多,因为不需要GDI+
- 图标还支持8位透明度(就像PNG一样)
- 图标可以是任何像素大小(就像PNG一样)
- 图标可以很容易地作为资源嵌入到可执行文件中
- 图标可以通过Visual Studio进行编辑
要从资源或文件加载图标,请使用:
LoadImage()
要绘制图标,请使用:
DrawIcon() or DrawIconEx()
相关文章:
- win32 GUI 在窗口中显示来自字符数组变量的文本
- 如何使用C Win32 API在Combobox中默认显示域值
- 如何使用C Win32 API在登录对话框中显示最后一个登录用户名
- 如何在不显示文本的情况下分配Win32编辑控件的窗口名称
- 使用C++Win32 GUI在窗口中显示文本
- Win32逻辑块预处理器显示为非活动状态
- Win32窗口未显示
- 如何在 Win32 API 的对话框中显示自定义消息
- 使用资源中的PNG图标显示Win32弹出菜单
- 在窗口标题和消息框中显示错误的语言:win32应用程序,opengl
- win32 GUI 在窗口中显示来自 int 变量的数字
- MFC:当进程在win32文本区域(MFC应用程序)中执行时,异步(并发)显示进程的输出
- win32, scrollwindowex(): 如何显示向下滚动后消失的"off window"的向上区域?
- 使用Richedit控件时,基于对话框的Win32 API程序将不会显示窗口
- 为什么当窗口在win32上失去焦点时,被覆盖的非客户端区域显示默认值
- Win32:是否可以显示窗口,但在任务栏中隐藏它
- Visual Studio 2012 for windows 8未显示win32控制台应用程序选项
- Win32 C++:使用开放文件名并显示位图文件
- 使用MFC或Win32显示来自内存缓冲区的所有位图类型
- Win32:显示监视器的捕获句柄