簡體   English   中英

從 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 的圖標的預期消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM