从 wxWidgets 中的 .DLL 加载图标

Load icon from .DLL in wxWidgets

我正在尝试通过从系统 DLL 加载来在 Windows 中加载一个 wxIcon(因为 mime 系统告诉我这种文件类型的图标在 DLL 中),例如。

wxIcon icon;
icon.LoadFile("C:\WINDOWS\system32\zipfldr.dll", wxICON_DEFAULT_TYPE);

这失败了,但我想知道除了求助于本机 Win32 函数之外,代码库中是否有任何方法可以加载它。

另外,如果有原生Win32函数,有谁知道它们是什么?

编辑:我尝试了以下但没有成功:

::wxInitAllImageHandlers();
wxMimeTypesManager manager;
wxFileType* type = manager.GetFileTypeFromExtension("sys");
wxIconLocation location;
if (type->GetIcon(&location))
{
  // location is something like C:\WINDOWS\system32\imageres.dll
  wxIcon icon;
  if (!icon.LoadFile(location.GetFileName(), wxBITMAP_TYPE_ICON /*I have tried wxICON_DEFAULT_TYPE too*/))
  {
    // Failed!
  }
}

编辑 2:作为对 VZ 的回应,我很遗憾地尝试了以下但没有成功:

::wxInitAllImageHandlers();
wxMimeTypesManager manager;
wxFileType* type = manager.GetFileTypeFromExtension("sys");
wxIconLocation location;
if (type->GetIcon(&location))
{
  // location is something like C:\WINDOWS\system32\imageres.dll,
  //with an appropriate index as retrieved by location.GetIndex(), which is -67.
  wxIcon icon(location);
  if (!icon.IsOk())
  {
    BREAK;
    // Failed!
  }
}

编辑 3: 感谢大家的帮助 - 如果我使用 wxBITMAP_TYPE_ICO 而不是 wxBITMAP_TYPE_ICON 就可以正常工作(注意 N),而且我将测试代码放入我的应用程序的构造函数中而不是 ::OnInit。它在 OnInit 中起作用,但在构造函数中不起作用,所以这是一个教训! 感谢大家的帮助和快速回复,一如既往的感激。

如果您指定类型 wxBITMAP_TYPE_ICO,它应该可以工作。

LoadFile() 的第一个参数必须在使用 wxBITMAP_TYPE_ICO 时指定图标资源 ID(这确实是您从文件加载图标时需要使用的,而不是当前模块的资源) ,即您还缺少最后的 ;N 部分,其中 NwxFileTypeInfo::GetIconIndex().

返回的值

但是为了避免明确地处理这个问题,你应该只使用 wxFileType::GetIcon() 并从它填充的 wxIconLocation 构造 wxIcon

例如,这个:

diff --git a/samples/minimal/minimal.cpp b/samples/minimal/minimal.cpp
index 0d91f7fc75..3623aacc56 100644
--- a/samples/minimal/minimal.cpp
+++ b/samples/minimal/minimal.cpp
@@ -123,6 +123,12 @@ bool MyApp::OnInit()
     if ( !wxApp::OnInit() )
         return false;

+    wxIcon icon(wxIconLocation(R"(c:\Windows\system32\imageres.dll)", -67));
+    if ( icon.IsOk() )
+    {
+        wxLogMessage("Loaded icon of size %d*%d", icon.GetWidth(), icon.GetHeight());
+    }
+
     // create the main application window
     MyFrame *frame = new MyFrame("Minimal wxWidgets App");

显示有关加载大小为 32 x 32 的图标的预期消息。