When you create an application, you often need external resources such as images, icons, sounds or documents to accompany it. In general, for a GUI application, you use an icon associated with the main window. You may also want to add one or more images to enhance the visual or functional aspects of your application. This is where resources come in. In fact, thanks to its ultra-simplified resource management, xtd provides easy access to your resources. Through an automatically-generated static class, you can access your resources without worrying about their location, which may differ depending on the platform used.
Resources declarationIn general, the best way to add resources is to create a resources
folder next to the src
folder. Copy your resources files into this folder. Then, using CMake
commands specific to xtd
, simply describe the resources to be added to your application.
Although you can add the resource command (which is the only command for adding a resource) in your CMakeLists.txt
file, the best thing to do is to create a properties
folder next to the src
folder and describe your resources in the resources.cmake
file. When CMake generates your xtd project, the properties/resources.cmake
file will be detected automatically.
Below is an example of the properties/resources.cmake
file with some specific CMake commands to describe your resources.
# Resources file
# ==============
# Remarks
# This file generates the "properties/resources.hpp" file, which is used to access the resources.
# Icons
resource(xtd_forms "resources/xtd_forms.ico")
# Pictures
resource(gammasoft "resources/gammasoft.png")
resource(xtd "resources/xtd.gif")
# Documents
resource(information "resources/information.txt")
resource(readme "resources/readme.md")
# Sounds
resource(sound "resources/sound.wav")
Resources CMake Commands resource
your_project::properties::resources
.Generating your xtd
project with CMake
will automatically generate a static class your_project::properties::resources
containing the static properties for accessing resources. The resources
class will be generated in the propertes/resources.hpp
file. And this file will be automatically added to your project as a source.
#pragma region xtd generated code
#pragma once
#include <xtd/drawing/bitmap>
#include <xtd/drawing/icon>
#include <xtd/io/binary_reader>
#include <xtd/io/file_info>
#include <xtd/io/path>
#include <xtd/environment>
#include <xtd/not_implemented_exception>
#include <xtd/string>
namespace your_project::properties {
class resources final static_ {
public:
static const xtd::drawing::icon& xtd_forms() {
static auto icon = xtd::drawing::icon {xtd::io::path::combine(xtd::environment::get_folder_path(xtd::environment::special_folder::application_resources), "xtd_forms.ico")};
return icon;
}
static const xtd::drawing::bitmap& gammasoft() {
static auto bitmap = xtd::drawing::bitmap {xtd::io::path::combine(xtd::environment::get_folder_path(xtd::environment::special_folder::application_resources), "gammasoft.png")};
return bitmap;
}
static const xtd::drawing::bitmap& xtd() {
static auto bitmap = xtd::drawing::bitmap {xtd::io::path::combine(xtd::environment::get_folder_path(xtd::environment::special_folder::application_resources), "xtd.gif")};
return bitmap;
}
static const xtd::string& information() {
static auto text = xtd::io::file::read_all_text(xtd::io::path::combine(xtd::environment::get_folder_path(xtd::environment::special_folder::application_resources), "information.txt"));
return text;
}
static const xtd::string& readme() {
static auto text = xtd::io::file::read_all_text(xtd::io::path::combine(xtd::environment::get_folder_path(xtd::environment::special_folder::application_resources), "readme.md"));
return text;
}
static const xtd::object& sound() {
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::not_implemented);
}
};
}
#pragma endregion
Resources usage
All that's left is to exploit your resources in a way that's simple and transparent to your environment and OS. Just include the inlcude file propertes/resources.hpp
and use its properties. By the way, a resource is always read-only.
#include "../properties/resources.hpp"
#include <xtd/xtd>
namespace your_project {
class form1 : public xtd::forms::form {
public:
form1() {
text("form1");
client_size({800, 450});
main_tab_control.dock(xtd::forms::dock_style::fill).parent(*this);
main_tab_control.tab_pages().push_back("Information");
main_tab_control.tab_pages().push_back("Gammasoft");
main_tab_control.tab_pages().push_back("Read me");
main_tab_control.tab_pages().push_back("xtd");
information_label.parent(main_tab_control.tab_pages()[0]);
information_label.dock(xtd::forms::dock_style::fill);
gammasoft_picture_box.parent(main_tab_control.tab_pages()[1]);
gammasoft_picture_box.dock(xtd::forms::dock_style::fill);
gammasoft_picture_box.size_mode(picture_box_size_mode::center_image);
readme_label.parent(main_tab_control.tab_pages()[2]);
readme_label.dock(xtd::forms::dock_style::fill);
xtd_picture_box.parent(main_tab_control.tab_pages()[3]);
xtd_picture_box.dock(xtd::forms::dock_style::fill);
xtd_picture_box.size_mode(picture_box_size_mode::center_image);
information_label.text(properties::resources::information());
gammasoft_picture_box.image(properties::resources::gammasoft());
readme_label.text(properties::resources::readme());
xtd_picture_box.image(properties::resources::xtd());
}
static auto main() -> void {
application::run(form1 {});
}
private:
tab_control main_tab_control;
label information_label;
picture_box gammasoft_picture_box;
label readme_label;
picture_box xtd_picture_box;
};
}
startup_(your_project::form1::main);
Resources types
xtd resources can handle different types of resources: audio, icons, images and text.
Audio Format File extensions Wave .wav Icon Format File extensions Window icon .ico macOS icon .icns Image Format File extensions ANImation .ani BitMaP .bmp CURsor .cur Embeded Map File .emf Exchangeable Image File Format .exif Graphics Interchange Format .gif Intuit Interchange Format .iif JPEG file .jpg JPEG file .jpeg apple PiCTure .pct PCX file .pcx apple PICTure .pict Portable Network Graphics .png Portable aNy Map .pnm Portable PixMap .ppm TGA file .tga Tag Image File format .tif Tag Image File Format .tiff Windows MetaFile .wmf X BitMap .xbm X PixMap .xpm Text Format File extensions MarkDown .md TeXT .txt TEXT .textxtd::array<xtd::byte>
.CMakeLists.txt
and don't add a properties/resources.cmake
file, then xtd won't generate anything.Some xtd examples use resources :
and more see xtd.examples.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4