{"id":4056,"date":"2022-01-06T09:28:38","date_gmt":"2022-01-06T09:28:38","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4056"},"modified":"2022-01-06T09:28:40","modified_gmt":"2022-01-06T09:28:40","slug":"8b-wxwidgets-wxbitmapbutton","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/06\/8b-wxwidgets-wxbitmapbutton\/","title":{"rendered":"8b.wxWidgets &#8211; wxBitmapButton"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>We use wxBitmapButton when we want to show an image on a button. We can also show text along with the image, in which case we can set the text alignment. The code below uses 3 images which are toggled on the same button every time it is clicked.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bitmapbutton.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass BitmapButton: public wxFrame {\n\n    public:\n      BitmapButton(const wxString &amp;title);\n      void onClick(wxCommandEvent &amp;event);\n\n    private:\n      wxBitmapButton *mBtn;\n      int mCurrImage;\n\n};\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">bitmapbutton.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;bitmapbutton.h&quot;\n\n\nBitmapButton::BitmapButton(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n \n    wxPanel *panel = new wxPanel(this, -1);    \n    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n    panel-&gt;SetSizer(vbox);\n\n    mBtn = new wxBitmapButton(panel, -1, wxBitmap(wxT(&quot;1.png&quot;), wxBITMAP_TYPE_PNG), wxDefaultPosition);\n\n    vbox-&gt;Add(mBtn, 1, wxEXPAND);\n    mCurrImage = 0;\n\n    mBtn-&gt;Bind(wxEVT_BUTTON, &amp;BitmapButton::onClick, this);\n    Center();\n\n}\n\nvoid BitmapButton::onClick(wxCommandEvent &amp;event) {\n\tmCurrImage ++;\n\tif (mCurrImage &gt; 2)\n\t\tmCurrImage = 0;\n\tif (mCurrImage == 0)\n\t\tmBtn-&gt;SetBitmapLabel(wxBitmap(wxT(&quot;1.png&quot;), wxBITMAP_TYPE_PNG));\n\telse if (mCurrImage == 1)\n\t\tmBtn-&gt;SetBitmapLabel(wxBitmap(wxT(&quot;2.png&quot;), wxBITMAP_TYPE_PNG));\n\telse if (mCurrImage == 2)\n\t\tmBtn-&gt;SetBitmapLabel(wxBitmap(wxT(&quot;3.png&quot;), wxBITMAP_TYPE_PNG));\n\n}\n\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t        virtual bool OnInit();\n};\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;bitmapbutton.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    BitmapButton *app = new BitmapButton(wxT(&quot;BitmapButton&quot;));\n    app-&gt;Show(true);\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-14-56-34.png\" alt=\"\" class=\"wp-image-4059\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-14-56-34.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-14-56-34-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>The images used can be downloaded from below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"50\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/1.png\" alt=\"\" class=\"wp-image-4060\"\/><figcaption>1.png<br><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"50\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/2.png\" alt=\"\" class=\"wp-image-4061\"\/><figcaption>2.png<br><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"50\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/3.png\" alt=\"\" class=\"wp-image-4062\"\/><figcaption>3.png<br><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We use wxBitmapButton when we want to show an image on a button. We can also show text along with the image, in which <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/06\/8b-wxwidgets-wxbitmapbutton\/\" title=\"8b.wxWidgets &#8211; wxBitmapButton\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4056"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4056\/revisions"}],"predecessor-version":[{"id":4064,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4056\/revisions\/4064"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}