{"id":3983,"date":"2021-12-26T04:22:03","date_gmt":"2021-12-26T04:22:03","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3983"},"modified":"2021-12-26T04:22:04","modified_gmt":"2021-12-26T04:22:04","slug":"6f-wxwidgets-using-window-identifiers","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/26\/6f-wxwidgets-using-window-identifiers\/","title":{"rendered":"6f. wxWidgets &#8211; Using Window Identifiers"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Every control or widget that you create is given an id. An id can be system-generated or it can be a predefined system id or you can give it an id of your choice. When you give a widget an id of -1 or<em> wxID_ANY<\/em>, then the system generates an id for that widget. System generated ids are negative eg. -1002, -2019 . <\/p>\n\n\n\n<p>There are certain ids which are predefined like <em>wxID_EXIT<\/em> or <em>wxID_NEW<\/em>. The functions of these ids are preset in the system so they should not be used for something else than what they are meant to represent.<\/p>\n\n\n\n<p>You can define your own ids as integer constants. By convention all user-defined ids should start from 101 onwards. You must ensure that all widgets have unique ids otherwise duplicate ids can cause runtime crashes.<\/p>\n\n\n\n<p>The code below creates three buttons with ids generated in the three ways described above.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">windows-ids.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 WindowsIds: public wxFrame {\n\n    public:\n      WindowsIds(const wxString &amp;title);\n      void OnClick(wxCommandEvent &amp;event);\n \n};\nconst int ID_BUTTON =101;\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">windows-ids-.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;windows-ids.h&quot;\n\n\nWindowsIds::WindowsIds(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n \n    wxPanel *panel = new wxPanel(this, -1);    \n    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n    panel-&gt;SetSizer(hbox);\n\n    wxButton *btn1 = new wxButton(panel, -1, wxT(&quot;Auto Generated Id&quot;));\n    wxButton *btn2 = new wxButton(panel, wxID_EXIT, wxT(&quot;System Id&quot;));\n    wxButton *btn3 = new wxButton(panel, ID_BUTTON, wxT(&quot;User defined id&quot;));\n   \n    hbox-&gt;Add(btn1, 1, wxEXPAND | wxALL, 5);\n    hbox-&gt;Add(btn2, 1, wxEXPAND | wxALL, 5);\n    hbox-&gt;Add(btn3, 1, wxEXPAND | wxALL, 5);\n\n    btn1-&gt;Bind(wxEVT_BUTTON, &amp;WindowsIds::OnClick, this);\n    btn2-&gt;Bind(wxEVT_BUTTON, &amp;WindowsIds::OnClick, this);\n    btn3-&gt;Bind(wxEVT_BUTTON, &amp;WindowsIds::OnClick, this);\n\n    Center();\n\n}\n\nvoid WindowsIds::OnClick(wxCommandEvent &amp;event) {\n\twxString strId = wxString::Format(wxString(&quot;%d&quot;), event.GetId());\n        wxMessageBox(strId);\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<\/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: plain; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;windows-ids.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    WindowsIds *app = new WindowsIds(wxT(&quot;Windows Ids&quot;));\n    app-&gt;Show(true);\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\/2021\/12\/Screenshot-from-2021-12-26-09-48-52.png\" alt=\"\" class=\"wp-image-3988\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-09-48-52.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-09-48-52-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"120\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-09-49-23.png\" alt=\"\" class=\"wp-image-3989\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"116\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-09-49-37.png\" alt=\"\" class=\"wp-image-3990\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"109\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-09-49-50.png\" alt=\"\" class=\"wp-image-3991\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Every control or widget that you create is given an id. An id can be system-generated or it can be a predefined system id <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/26\/6f-wxwidgets-using-window-identifiers\/\" title=\"6f. wxWidgets &#8211; Using Window Identifiers\">[&#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-3983","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\/3983","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=3983"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3983\/revisions"}],"predecessor-version":[{"id":3993,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3983\/revisions\/3993"}],"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=3983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}