我有一个小应用程序,我想在其中创建一些按钮、工具栏等,但我不想使用预先存在的 GTK 图标主题,我想创建自己的。所以我寻找教程之类的,但事实证明,网上讨论得并不好。所以我尝试自己做一些事情:
头文件:
#include <gtkmm.h>
#include <string>
#include <iostream>
#include <errno.h>
class IconFactoryBuilder
{
public:
IconFactoryBuilder();
~IconFactoryBuilder();
void RegisterNewIcons(std::string pPath);
private:
Glib::RefPtr<Gtk::IconFactory> mCustomFactory;
};
cp文件:
#include <IconFactoryBuilder.h>
IconFactoryBuilder::IconFactoryBuilder() {
mCustomFactory = Gtk::IconFactory::create();
}
IconFactoryBuilder::~IconFactoryBuilder() {
}
void IconFactoryBuilder::RegisterNewIcons(std::string pPath) {
Glib::RefPtr<Gtk::IconSet> iconSet = Gtk::IconSet::create();
Gtk::IconSource someSource;
try{
Gtk::Image *someImage=Gtk::manage(new Gtk::Image(pPath+"appbar.at.png"));
someImage->set_pixel_size(Gtk::IconSize(48));
someSource.set_pixbuf(someImage->get_pixbuf());
someSource.set_size(Gtk::ICON_SIZE_DIALOG);
someSource.set_size_wildcarded();
}
catch(const Glib::Exception &ex) {
std::cerr << "An error occurred while opening the icon file!" << strerror(errno) << std::endl;
}
catch(...) {
std::cerr << "Unknown Error!" << std::endl;
}
iconSet->add_source(someSource);
const Gtk::StockID somestock("MyNewIcon");
Gtk::Stock::add(Gtk::StockItem(somestock, "somelabel"));
mCustomFactory->add(somestock, iconSet);
mCustomFactory->add_default();
}
但是现在我很困惑,因为我真的不知道如何调用我创建的这个新图标。我也不知道上面写的代码是否足以真正找到图标。