简体   繁体   English

wxWidgets在编译时包含图像

[英]wxWidgets include image during compile time

I've been trying to make a simple wxWidgets program with just a button with a picture on it. 我一直在尝试制作一个简单的wxWidgets程序,只带有一个带有图片的按钮。 I've been able to make the button with the image on it easily enough, but my problem arises when including it. 我已经能够很轻松地使带有图像的按钮,但是包含它时出现了我的问题。

So far, I've only been able to fetch the image during run-time (the image has to be in the same folder as the .exe file; otherwise, I get error 2: the system cannot find the file specified). 到目前为止,我只能在运行时获取图像(该图像必须与.exe文件位于同一文件夹中;否则,我会收到错误2:系统找不到指定的文件)。 With this method, I have no problems -- the program works just fine. 使用这种方法,我没有问题-程序运行正常。 What I'm trying to do, however, is to #include the file so that it is embedded during compile-time, so that it doesn't need to be available during run-time. 但是,我想做的是#include该文件,以便在编译时将其嵌入,从而使其在运行时不需要可用。

I've tried #including the file (both as .png and as .xpm), and I've also tried adding it to the resource includes (this is on Visual Studio 2017). 我尝试过#include该文件(.png和.xpm两者),并且还尝试将其添加到资源包括(这在Visual Studio 2017中)。 Neither of these worked -- the first method still required the image to be in the same folder, and the second failed during compilation (as far as I can tell, it wasn't able to read the .xpm file). 这两种方法都不起作用-第一种方法仍然要求图像位于同一文件夹中,第二种方法在编译期间失败(据我所知,它无法读取.xpm文件)。

Here is the relevant code, if it helps: 如果有帮助,请参见以下相关代码:

/*relevant includes*/
#include "happyFace.png" //this isn't working. the file is still needed
||
#include "happyFace.xpm" //ditto
/*I have also tried putting these lines in the resource includes.*/


/*code*/
wxInitAllImageHandlers();
wxBitmap bitmap("happyFace.xpm", wxBITMAP_TYPE_XPM); //only works in same directory at run-time
||
wxBitmap bitmap("happyFace.png", wxBITMAP_TYPE_PNG); //ditto

wxButton *button = new wxButton(this, ID_BMP_BUTTON);
button->SetBitmap(bitmap);
//the rest of the button programming and stuff

Sorry if I haven't provided enough information; 对不起,如果我没有提供足够的信息; I can provide more if necessary. 如果需要,我可以提供更多。 I would really appreciate any help. 我真的很感谢您的帮助。 Thanks! 谢谢!

Two possibilities... Number 1 is simplest. 两种可能性...数字1最简单。 It's been a long time since I wrote the code I'm looking at, so the details are fuzzy. 自从我写了我要看的代码以来已经有很长时间了,所以细节很模糊。

  1. In Visual Studio, Solution Explorer, add the image into the resource files . 在Visual Studio的解决方案资源管理器中,将图像添加到resource files Assume the name of the resourse is sample.rc . 假设资源名称为sample.rc Then it can be used like so to set the main icon... 然后可以像这样使用它来设置主图标...

     SetIcon(wxICON(sample)); 

Method 1 must be used in order for MS Windows Explorer to display the main icon. 为了使MS Windows资源管理器显示主图标,必须使用方法1。 I do not remember how to use .rc resources for other things, but it should be easy to figure out. 我不记得如何将.rc资源用于其他用途,但是应该很容易弄清楚。

  1. I did it this way before I discovered VS resource (.rc) files. 在发现VS资源(.rc)文件之前,我是用这种方式完成的。 Compile the file-image into the program "by hand." “手动”将文件映像编译到程序中。 In other words, write a program that will read an image file and produce bit-for-bit copy in a .cpp file. 换句话说,编写一个程序,该程序将读取图像文件并在.cpp文件中逐位复制。 Then compile that .cpp into the program. 然后将该.cpp编译到程序中。 Here I have the file-image in memory as an object named dj::main_cursor. 在这里,我将文件图像作为一个名为dj :: main_cursor的对象存储在内存中。 Note that the in-memory version is a bit-for-bit copy of a .cur file. 请注意,内存版本是.cur文件的逐位副本。

     dj::captured_file &c1file(dj::main_cursor); wxMemoryInputStream cistr(c1file.contents, c1file.size); cursor1 = wxCursor(wxImage(cistr, wxBITMAP_TYPE_CUR)); 

    FYI, I defined the structure dj::captured_file like so: 仅供参考,我定义了dj :: captured_file结构,如下所示:

     struct captured_file { const char *name; const unsigned long size; const void *contents; captured_file(const char*fn, size_t sz, const void*c) : name(fn) , contents(c) , size(sz) {} }; 

See also, Embedding PNG Images into Windows RC Files 另请参阅将PNG图像嵌入Windows RC文件

I found some other documentation. 我找到了其他一些文档。

Resources and Application Icon All applications using wxMSW should have a Windows resource file (.rc extension) and this file should include include/wx/msw/wx.rc file which defines resources used by wxWidgets itself. 资源和应用程序图标所有使用wxMSW的应用程序都应具有Windows资源文件(扩展名为.rc),并且该文件应包括include / wx / msw / wx.rc文件,该文件定义了wxWidgets本身使用的资源。

Among other things, wx.rc defines some standard icons, all of which have names starting with the "wx" prefix. 除其他外,wx.rc定义了一些标准图标,所有这些图标的名称均以“ wx”前缀开头。 This normally ensures that any icons defined in the application's own resource file come before them in alphabetical order which is important because Explorer (Windows shell) selects the first icon in alphabetical order to use as the application icon which is displayed when viewing its file in the file manager. 通常,这可以确保应用程序自己的资源文件中定义的任何图标都按字母顺序排在它们前面,这很重要,因为资源管理器(Windows Shell)选择了按字母顺序排列的第一个图标作为应用程序图标,该图标在应用程序中查看其文件时显示。文件管理器。 So if all the icons defined in your application start with "x", "y" or "z", they won't be used by Explorer. 因此,如果您的应用程序中定义的所有图标都以“ x”,“ y”或“ z”开头,则资源管理器将不会使用它们。 To avoid this, ensure that the icon which is meant to be used as the main application icon has a name preceding "wxICON" in alphabetical order. 为避免这种情况,请确保要用作主应用程序图标的图标的名称按字母顺序在“ wxICON”之前。

http://docs.wxwidgets.org/3.1.0/page_port.html http://docs.wxwidgets.org/3.1.0/page_port.html

Here is how you should do it: 这是您应该如何做:

#include "happyFace.xpm"
wxBitmap bitmap = wxBitmap( happyFace ); // assuming the variable name is "happyFace" inside the xpm

Then you will use bitmap object just like usual. 然后,您将像往常一样使用位图对象。 Assuming that the file happyFace.xpm is available for compilation. 假设文件happyFace.xpm可用于编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM