WxWidgets - 无法加载图像 [英] WxWidgets - Unable to load images
问题描述
我最近开始使用 WxWidgets (2.9.4) 并且正在学习我找到的教程,但我似乎无法加载任何图像.我已经正确使用了处理程序(对于 PNG)并且问题发生在运行时.下面是尝试运行程序时显示的弹出窗口的图像.
I recently started working with WxWidgets (2.9.4) and was working through a tutorial I found, but it seems that I'm unable to load any images. I've already properly used the handler (for PNG) and the problem happens at run-time. Below is an image of the popup that is displayed when attempting to run the program.
代码如下:
wxPNGHandler *handler = new wxPNGHandler;
wxImage::AddHandler(handler);
wxBitmap exit;
exit.LoadFile(wxT("exit.png"), wxBITMAP_TYPE_PNG);
wxToolBar *toolbar = CreateToolBar();
toolbar->AddTool(wxID_EXIT, exit, wxT("Exit"));
toolbar->Realize();
Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(mainWindow::exitProg));
感谢任何帮助.
我忘了提到当我点击取消时,会发生这种情况:
I forgot to mention that when I click Cancel, this happens:
我把exit.png文件放在了build目录(/Debug或/Release)和源代码目录下,但还是没看到.
I placed the exit.png file in the build directory (/Debug or /Release) as well as the source code directory, but it still has yet to see it.
推荐答案
你的工作目录是什么?
如果您使用的是 Visual Studio 并使用界面(F5 或 ctrl-F5 或工具栏中的小运行按钮)运行,那么您的工作目录是包含项目文件的文件夹.因此,请尝试将您的图像文件复制到那里.
If you are using visual studio and running using the interface ( F5 or ctrl-F5 or the little run button in the toolbar ) then your working directory is the folder containing the project file. So try copying your image file there.
或者打开一个命令窗口,cd 到您的构建目录之一,然后从命令行运行您的应用程序.
Or open a command window, cd to one of your build directories, and run your app from the command line.
一般来说,为了避免此类问题,我更改了项目属性,以便可执行文件不存储在构建文件夹之一中,而是存储在一个新文件夹中(我通常称之为bin"——我的 unix 根目录是显示!)并更改调试属性,使工作目录为 bin 文件夹.
In general, to avoid this sort of problem, I alter the project properties so that the executable is NOT stored in one of the build folders, but in a new folder ( which I usually call 'bin' - my unix roots are showing! ) and also alter the debugging properties so that the working directory is the bin folder.
这种技术有几个优点:
- 发行版和调试版都使用相同的文件夹,因此您只需要一份任何额外文件的副本,例如您的图像文件.
- 很容易查看工作目录中的可执行文件和额外文件,而不会被构建文件夹中的所有 .obj 文件分心
恕我直言,在维护非默认项目属性方面花费一些额外的麻烦是值得的.
IMHO this is well worth the little extra trouble in maintaining non default project properties.
这篇关于WxWidgets - 无法加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!