wxWidgets 在编译时包含图像
wxWidgets include image during compile time
我一直在尝试制作一个简单的 wxWidgets 程序,它只有一个带有图片的按钮。我已经能够很容易地制作带有图像的按钮,但是在包含它时出现了我的问题。
到目前为止,我只能在 运行 时间内获取图像(图像必须与 .exe 文件位于同一文件夹中;否则,我会收到错误 2:系统找不到指定的文件)。使用这种方法,我没有任何问题——程序运行良好。然而,我想要做的是 #include 文件,以便它在编译时嵌入,这样它就不需要在 运行 时间期间可用。
我试过 #include 文件(.png 和 .xpm),我也试过将它添加到资源包含中(这是在 Visual Studio 2017 年)。这些都不起作用——第一种方法仍然要求图像位于同一文件夹中,而第二种方法在编译过程中失败了(据我所知,它无法读取 .xpm 文件)。
下面是相关代码,如果有帮助的话:
/*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
抱歉,如果我没有提供足够的信息;如有必要,我可以提供更多。我真的很感激任何帮助。谢谢!
两种可能性...第 1 种是最简单的。看的代码好久没写了,所以细节比较模糊。
在Visual Studio、解决方案资源管理器中,将图片添加到resource files
。假设资源的名称是 sample.rc
。然后就可以这样设置主图标了...
SetIcon(wxICON(sample));
必须使用方法 1 才能使 MS Windows Explorer 显示主图标。我不记得如何将 .rc 资源用于其他用途,但应该很容易弄清楚。
在我发现VS资源(.rc)文件之前,我就是这样做的。将 file-image 编译成程序 "by hand." 换句话说,编写一个程序来读取图像文件并在 .cpp 文件中生成 bit-for-bit 副本。然后将该 .cpp 编译到程序中。在这里,我在内存中将 file-image 作为一个名为 dj::main_cursor 的对象。请注意,in-memory 版本是 .cur 文件的 bit-for-bit 副本。
dj::captured_file &c1file(dj::main_cursor);
wxMemoryInputStream cistr(c1file.contents, c1file.size);
cursor1 = wxCursor(wxImage(cistr, wxBITMAP_TYPE_CUR));
仅供参考,我这样定义结构 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)
{}
};
另见,Embedding PNG Images into Windows RC Files
我找到了一些其他文档。
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.
Among other things, wx.rc defines some standard icons, all of which
have names starting with the "wx" prefix. 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. So if all the icons defined in your application start
with "x", "y" or "z", they won't be used by Explorer. 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.
以下是您应该如何操作:
#include "happyFace.xpm"
wxBitmap bitmap = wxBitmap( happyFace ); // assuming the variable name is "happyFace" inside the xpm
然后您将像往常一样使用位图对象。假设文件 happyFace.xpm 可用于编译。
我一直在尝试制作一个简单的 wxWidgets 程序,它只有一个带有图片的按钮。我已经能够很容易地制作带有图像的按钮,但是在包含它时出现了我的问题。
到目前为止,我只能在 运行 时间内获取图像(图像必须与 .exe 文件位于同一文件夹中;否则,我会收到错误 2:系统找不到指定的文件)。使用这种方法,我没有任何问题——程序运行良好。然而,我想要做的是 #include 文件,以便它在编译时嵌入,这样它就不需要在 运行 时间期间可用。
我试过 #include 文件(.png 和 .xpm),我也试过将它添加到资源包含中(这是在 Visual Studio 2017 年)。这些都不起作用——第一种方法仍然要求图像位于同一文件夹中,而第二种方法在编译过程中失败了(据我所知,它无法读取 .xpm 文件)。
下面是相关代码,如果有帮助的话:
/*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
抱歉,如果我没有提供足够的信息;如有必要,我可以提供更多。我真的很感激任何帮助。谢谢!
两种可能性...第 1 种是最简单的。看的代码好久没写了,所以细节比较模糊。
在Visual Studio、解决方案资源管理器中,将图片添加到
resource files
。假设资源的名称是sample.rc
。然后就可以这样设置主图标了...SetIcon(wxICON(sample));
必须使用方法 1 才能使 MS Windows Explorer 显示主图标。我不记得如何将 .rc 资源用于其他用途,但应该很容易弄清楚。
在我发现VS资源(.rc)文件之前,我就是这样做的。将 file-image 编译成程序 "by hand." 换句话说,编写一个程序来读取图像文件并在 .cpp 文件中生成 bit-for-bit 副本。然后将该 .cpp 编译到程序中。在这里,我在内存中将 file-image 作为一个名为 dj::main_cursor 的对象。请注意,in-memory 版本是 .cur 文件的 bit-for-bit 副本。
dj::captured_file &c1file(dj::main_cursor); wxMemoryInputStream cistr(c1file.contents, c1file.size); cursor1 = wxCursor(wxImage(cistr, wxBITMAP_TYPE_CUR));
仅供参考,我这样定义结构 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) {} };
另见,Embedding PNG Images into Windows RC Files
我找到了一些其他文档。
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.
Among other things, wx.rc defines some standard icons, all of which have names starting with the "wx" prefix. 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. So if all the icons defined in your application start with "x", "y" or "z", they won't be used by Explorer. 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.
以下是您应该如何操作:
#include "happyFace.xpm"
wxBitmap bitmap = wxBitmap( happyFace ); // assuming the variable name is "happyFace" inside the xpm
然后您将像往常一样使用位图对象。假设文件 happyFace.xpm 可用于编译。