简体   繁体   English

为wxAuiToolBar添加我自己的图标

[英]Adding my own Icon for wxAuiToolBar

I want to add a button to an wxAuiToolBar that shows an icon that I created myself (I have it as the file myicon.png with resolution 24x24). 我想向wxAuiToolBar添加一个按钮,该按钮显示我创建的图标(我将其作为分辨率为24x24的myicon.png文件使用)。

The best I manged so far is to convert to bmp-file and use the following code for wxsmith: 到目前为止,我管理的最好的方法是将其转换为bmp文件,并对wxsmith使用以下代码:

<wxsmith>
    <object class="wxFrame" name="MyFrame">
        <size>800,640</size>
        <object class="wxAuiManager" variable="aui_manager" member="yes">
            <object class="AuiManagerItem">
                <object class="wxAuiToolBar" name="ID_AUITOOLBAR" variable="toolbar" member="yes">
                    <pos>90,3</pos>
                    <object class="AuiToolBarItem">
                        <object class="wxAuiToolBarItem" name="ID_AUITOOLBARMEASURE" variable="AuiToolBarMeasure" member="yes">
                            <bitmap>/home/username/project/icon.bmp</bitmap>
                            <handler function="OnTest" entry="EVT_TOOL" />
                        </object>
                        <label>Test</label>
                    </object>
                </object>
            </object>
        </object>
    </object>
</wxsmith>

Leading to the following source code: 导致以下源代码:

toolbar->AddTool(ID_AUITOOLBAR1, _("Test"),
    wxBitmap(wxImage(_T("/home/username/project/icon.bmp"))), wxNullBitmap, wxITEM_NORMAL, wxEmptyString, wxEmptyString, NULL);

However the transparency information is lost. 但是,透明度信息会丢失。

If I use the png-file or convert to xpm then I get a runtime error: "../src/gtk/bitmap.cpp(626): assert "image.IsOk()" failed in wxBitmap(): invalid image" 如果我使用png文件或转换为xpm,则会收到运行时错误:“ ../src/gtk/bitmap.cpp(626):断言” image.IsOk()“在wxBitmap()中失败:无效图像”

Currently I'm trying to get it working with WxSmith, but if that turns out not to be possible I'd also be happy if anyone can help me to get it working without. 目前,我正在尝试使其与WxSmith一起工作,但是如果事实证明不可能,那么如果有人可以帮助我让它在没有工作的情况下,我也将非常高兴。

In order to load PNG files during run-time you need to register the PNG image handler which can be done with wxInitAllImageHandlers() . 为了在运行时加载PNG文件,您需要注册PNG图像处理程序,可以使用wxInitAllImageHandlers()完成此操作。 And you still should actually check that bitmap creation succeeded because it's always possible that the file wasn't found or corrupted. 而且您实际上仍然应该检查位图创建是否成功,因为总是有可能找不到文件或文件损坏。

Alternatively, you can embed PNG directly in your program and then use the convenient wxBITMAP_PNG() macro to load it. 另外,您可以将PNG直接嵌入程序中,然后使用方便的wxBITMAP_PNG()宏加载它。 In this case you can usually omit the error handling. 在这种情况下,通常可以省略错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM