本文介绍了如何在静态库 (Visual Studio) 中嵌入图标 (.ico)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 c++ (Win32 API) 静态库中嵌入资源(例如图标、对话框)?我的目的是在静态库中嵌入一个图标,使使用 LoadIcon 的函数可以像普通 .exe 一样工作,因此主应用程序只能链接到静态库并包含一个头文件,而无需添加其他文件,例如 .rc 文件或 .ico 文件等.显然,使用静态库的主应用程序没有此资源,因此 LoadIcon 将失败,但是我想知道是否有解决方法可以使其工作.只要标准 API 调用(例如 LoadIcon)可以工作,带有图标数据的静态数组就可以工作.

Is there a way to embed resources (such as icons, dialogs) in a c++ (Win32 API) static library?My purpose is to embed an icon in the static library in a way that functions that use LoadIcon will work as if it was a normal .exe so the main application can only link to the static library and include a header file, with no requirement to add other files such as .rc files, or .ico files, etc.Clearly the main application who uses the static library doesn't have this resource so LoadIcon will fail, however I was wondering if there is a workaround to make it work. A static array with the icon data can work as long as the standard API calls (such as LoadIcon) will work.

进一步解释一下,使用静态库的人只有 2 个文件:.lib 和 .h,不会有任何 .rc 文件.

To explain further, the person who will be using the static library will only have 2 files: .lib and .h and will not have any .rc file.

推荐答案

我发布了一个答案,因为经过一些研究我找到了一种方法.使用我的方法,图标可以用作静态库的组成部分,并且此类库可以被任何类型的应用程序使用).另请参阅:https://www.codeproject.com/Articles/1275122/How-to-embed-resources-in-a-Static-Library

I am posting an answer because after some research I found a way. Using my method, an icon can be used as an integral part of a static library and such library can be used by any type of application).See also: https://www.codeproject.com/Articles/1275122/How-to-embed-resources-in-a-Static-Library

  1. Icon 被转换成一个静态的 BYTE 数组.bin2c 可用于此目的.
  2. 数据被转换成 HICON 句柄.我是这样做的:

  1. Icon is converted to a static array of BYTE. bin2c can be used for that.
  2. Data is converted into a HICON handle. Here is how I have done that:

HICON GetIcon()
{
   DWORD dwTmp;
   int offset;
   HANDLE hFile;
   HICON hIcon = NULL;
   offset = LookupIconIdFromDirectoryEx(s_byIconData, TRUE, 0, 0, LR_DEFAULTCOLOR);
   if (offset != 0)
   {
      hIcon = CreateIconFromResourceEx(s_byIconData + offset, 0, TRUE, 0x00030000, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
   }
   return hIcon;
}

  • 使用 GetIcon 代替 LoadIcon.而不是调用:

  • GetIcon is used instead of LoadIcon.Instead of calling:

    m_hIcon = ::LoadIcon(hInstanceIcon, MAKEINTRESOURCE(pXMB->nIcon));

    我打电话

    m_hIcon = GetIcon()
    

    这篇关于如何在静态库 (Visual Studio) 中嵌入图标 (.ico)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-01 22:50