返回介绍

10.7 自定义 wxWidgets 提供的小图片

发布于 2025-03-08 15:26:47 字数 2756 浏览 0 评论 0 收藏 0

wxArtProvider 这个类允许你更改 wxWidgets 默认提供的那些小图片,比如 wxWidgets HTML 帮助阅读器中或者默认的 Log 对话框中使用的图片。

wxWidgets 提供了一个标准的 wxArtProvider 对象,并且体系内的一些需要使用图标和小图片的地方都调用了这个类的 wxArtProvider::GetBitmap 和 wxArtProvider::GetIcon 函数。

小图片是由两个标识符决定的:主标识符(wxArtID) 和客户区标识符(wxArtClient).其中客户区标识符只在同一个主标识符在不同的窗口中需要不同的图片的时候才使用,比如,wxHTML 帮助窗口使用的图标使用下面的代码取得的:

wxBitmap bmp = wxArtProvider::GetBitmap(wxART_GO_BACK,wxART_TOOLBAR);

如果你想浏览所有 wxWidgets 提供的小图片以及它们的标识符,你可以编译和运行 wxWidgets 自带的 samples/artprov 中的例子,它的外观如下图所示:

要替换 wxWidgets 提供的这些小图片,你需要实现一个 wxArtProvider 的派生类,重载其中的 CreateBitmap 函数,然后在 OnInit 函数中调用 wxArtProvider::PushProvider 以便让 wxWidgets 知道.下面的这个例子替换了 wxHTML 帮助窗口中的大部分默认的图标:

// 新的图标
#include "bitmaps/helpbook.xpm"
#include "bitmaps/helppage.xpm"
#include "bitmaps/helpback.xpm"
#include "bitmaps/helpdown.xpm"
#include "bitmaps/helpforward.xpm"
#include "bitmaps/helpoptions.xpm"
#include "bitmaps/helpsidepanel.xpm"
#include "bitmaps/helpup.xpm"
#include "bitmaps/helpuplevel.xpm"
#include "bitmaps/helpicon.xpm"
#include "wx/artprov.h"
class MyArtProvider : public wxArtProvider
{
protected:
    virtual wxBitmap CreateBitmap(const wxArtID& id,
                                  const wxArtClient& client,
                                  const wxSize& size);
};
// 新的 CreateBitmap 函数
wxBitmap MyArtProvider::CreateBitmap(const wxArtID& id,
                                     const wxArtClient& client,
                                     const wxSize& size)
{
    if (id == wxART_HELP_SIDE_PANEL)
        return wxBitmap(helpsidepanel_xpm);
    if (id == wxART_HELP_SETTINGS)
        return wxBitmap(helpoptions_xpm);
    if (id == wxART_HELP_BOOK)
        return wxBitmap(helpbook_xpm);
    if (id == wxART_HELP_FOLDER)
        return wxBitmap(helpbook_xpm);
    if (id == wxART_HELP_PAGE)
        return wxBitmap(helppage_xpm);
    if (id == wxART_GO_BACK)
        return wxBitmap(helpback_xpm);
    if (id == wxART_GO_FORWARD)
        return wxBitmap(helpforward_xpm);
    if (id == wxART_GO_UP)
        return wxBitmap(helpup_xpm);
    if (id == wxART_GO_DOWN)
        return wxBitmap(helpdown_xpm);
    if (id == wxART_GO_TO_PARENT)
        return wxBitmap(helpuplevel_xpm);
    if (id == wxART_FRAME_ICON)
        return wxBitmap(helpicon_xpm);
    if (id == wxART_HELP)
        return wxBitmap(helpicon_xpm);

    // Any wxWidgets icons not implemented here
    // will be provided by the default art provider.
    return wxNullBitmap;
}
// 你的初始化函数
bool MyApp::OnInit()
{
    ...
    wxArtProvider::PushProvider(new MyArtProvider);
    ...
    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文