Is there a way to load an icon from a memory file handler?
I am writing wxWidgets application where I am importing the .ICO file as a header. I am attempting to use a wxMemoryFSHandler to make this icon (and others as well) accessible as files. I am using the following code to do this:
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxMemoryFSHandler::AddFileWithMimeType(
"app_inactive.ico",
CsiWebAdmin_ico,
sizeof(CsiWebAdmin_ico),
"image/vnd.microsoft.icon");
Unfortunately, if I try to load an icon from this "file" as shown below, it does not work. As I stepped through the MSW source (wx 2.8.10), I can see that the开发者_如何学运维 loader never attempted to resolve the virtual file name.
wxIcon icon("memory:app_inactive.ico");
I have also tried the following:
wxIcon icon(wxIconLocation("memory:app_inactive.ico"));
and have encountered the same results.
I realise that I can use resources to load these files but I would still face the same dilemma when the time came to port my application to GTK. Is there something obvious that I am missing?
Are you trying to set application icons? Then see wxIconBundle. I use this piece of code:
wxFileName frameIconFile = your_resources_folder;
frameIconFile.SetFullName("appicon.ico");
wxIconBundle frameIcons(frameIconFile.GetFullPath(),wxBITMAP_TYPE_ICO);
mainFrame->SetIcons(frameIcons);
The icon bundle (it's a file with multiple icons) is loaded at application startup. To build the icon bundle I use IcoFX (http://icofx.ro/).
I have determined that there was no means of loading the icon set directly from the virtual file system. I have figured out, however, a means to do so as follows:
wxFileSystem file_system;
Csi::SharedPtr<wxFSFile> ico_file(
file_system.OpenFile(
make_wxString(config->get_control()->get_attr_wstr(L"icon"))));
if(ico_file != 0)
{
wxIconBundle icons;
wxInputStream *stream(ico_file->GetStream());
size_t image_count(wxImage::GetImageCount(*stream, wxBITMAP_TYPE_ICO));
wxImage image;
for(size_t i = 0; i < image_count; ++i)
{
if(image.LoadFile(*stream, wxBITMAP_TYPE_ICO, i))
{
wxIcon icon;
icon.CopyFromBitmap(wxBitmap(image));
icons.AddIcon(icon);
}
}
SetIcons(icons);
}
See png2wx.pl for an example of how to embed icons into an executable; it is then available as a C symbol, such as
wxMenuItem *w = some_menu->Append(id, _("text")); w->SetBitmap(*_img_exit_icon);
精彩评论