c++ - 将CBitmap转换为HICON并在状态栏上使用它

标签 c++ mfc statusbar

我找到了这个link,它向您展示了如何将CBitmap转换为HICON:

HICON HICONFromCBitmap(CBitmap& bitmap)
{
   BITMAP bmp;
   bitmap.GetBitmap(&bmp);

   HBITMAP hbmMask = ::CreateCompatibleBitmap(::GetDC(NULL), 
                                              bmp.bmWidth, bmp.bmHeight);

   ICONINFO ii = {0};
   ii.fIcon    = TRUE;
   ii.hbmColor = bitmap;
   ii.hbmMask  = hbmMask;

   HICON hIcon = ::CreateIconIndirect(&ii);
   ::DeleteObject(hbmMask);

   return hIcon;
}

所以我在我的应用程序中尝试了这个:
HICON hIcon = HICONFromCBitmap(m_mapMenuBitmap[5]);
VERIFY(hIcon);
m_StatusBar.GetStatusBarCtrl().SetIcon(paneProgressOrZoomFactor, hIcon);

有用:

Status Bar

在我的窗口期间,hIcon是否需要保持 Activity 状态?而且我必须释放它吗?

为了澄清起见,我的m_mapMeniBitmapsCBitmap对象的映射,并且它们确实仍然存在

最佳答案

在窗口期间,hIcon是否需要还活着?而且我必须释放它吗?

是的,是的!从documentation for CreateIconIndirect (爆炸中的我):

When you are finished using the icon, destroy it using the DestroyIcon function.



我的m_mapMenuBitmapsCBitmap对象的映射,它们确实保持 Activity 状态。

您可以在创建图标后释放这些CBitmap对象,但是,如您所正确记述的那样,它们仍保持“ Activity ”状态,因此,当不再需要它们时,应始终“杀死”它们。来自同一M / S文档:

The system copies the bitmaps in the ICONINFO structure before creating the icon or cursor.
...
The application must continue to manage the original bitmaps and delete them when they are no longer necessary.

关于c++ - 将CBitmap转换为HICON并在状态栏上使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289272/

相关文章:

C++ 控制台自由文本输入(最大字符大小)

c++ - 如何正确使用SDL_MOUSEBUTTONDOWN?

javascript - 可靠的跨浏览器设置状态栏文本的方式

android - 状态栏敏感信息,如 Telegram X

c++ - 如何使进程 B 仅在进程 A 处于事件状态时运行

c++ - MFC - 打印 - 从自定义页面设置对话框更改页面方向

c# - 将 MFC COM 服务器移植到托管代码的最佳方式

c++ - qsort线程安全吗?

iOS9:自定义UIWindow让状态栏消失

c++ - QT - 强制对象处理传入信号