title: newbie: Code to show/hide a STATIC TEXT object in Visual C++ MFC app
body: <p>I ran into a newbie problem with my first VC++ MFC app <em>(actually, I ran into many problems, but RTFM and DuckDuckGo helped to solve them without crying here for help. Except this one!)</em>. Bear in mind that I am playing with this as a tutorial for myself, kind of a <strong>learn by example</strong> project, and I have a few years of Win GUI app programming experience in Deplhi/Lazarus, and now I am attempting to transition into VC++, simply for my own curiosity. While I am also good with <strong>C language</strong> programming, I have significantly less experience with C++. So the new programming environment and the less-known language together pose as my obstacle.</p>
  <p>Here is what I did:<br />
  In a recently installed <strong>Visual Studio 2019 Community</strong> with only the <strong>Windows App development in C++</strong> components selected, started a new project, chose <strong>C++ MFC App</strong> <em>(Build apps with complex user interfaces that run on Windows.)</em>. Set application type to <strong>Dialog based</strong>, turn off all User Interface Features so only <strong>Thick frame</strong> is checked <em>(unchecked System-menu, unchecked About-box)</em>, turn off all Advanced Features so only <strong>Common Control Manifest</strong> is checked <em>(unck Printing and print preview, unck Activex controls, unck Support Restart Manager)</em>, clicked FINISH.<br />
  This prepared me an app with a single small main window, OK and Cancel buttons in its lower-right corner, and a STATIC TEXT item in the middle-center reading something like <em>&quot;TODO: add your own items here&quot;</em>. Project name is <strong>TutMFC01p</strong>.</p>
  <p>My goal was to hide that STATIC TEXT when I click one of the buttons, and make it visible again when I click the same button again.<br />
  It took me some time to realize that I should not fiddle with the OK and Cancel buttons to add them this functionality, and clicking either of these two buttons also quits my app <em>(hence, no chance to click again)</em>. So I placed a new button on the dialog and worked with that instead. Clicking my button while my app was running did absolutely nothing - which was exactly what I wanted.<br />
  Double-clicking my button in the <strong>Dialog Editor</strong> dropped me into <strong>Source Editor</strong> with a new function autogenerated at the bottom of <strong>TutMFC01pDlg.cpp</strong>.</p>
  <pre class="lang-cpp prettyprint-override"><code>void CTutMFC01pDlg::OnBnClickedButton1()
  {
    // TODO: Add your control notification handler code here
  }
  </code></pre>
  <p>Allrighty, so this is where I will add the code of what the button is supposed to do.<br />
  It also injected an ON_BN_CLICKED line to the MESSAGE MAP, which now looks like this.</p>
  <pre class="lang-cpp prettyprint-override"><code>BEGIN_MESSAGE_MAP(CTutMFC01pDlg, CDialogEx)
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(IDC_BUTTON1, &amp;CTutMFC01pDlg::OnBnClickedButton1)
  END_MESSAGE_MAP()
  </code></pre>
  <p>Allrighty again. So this is the way to tell the system that clicking my button should run the code given in CTutMFC01pDlg::OnBnClickedButton1().<br />
  The way I first tried to complete my goal was to alternate the STATIC TEXT object between the TRUE and FALSE value of the VISIBILE property upon the click of my button. A Delphi/Lazarus way of doing it is a single line of code like <code>mainform.mystatictext.visible := not mainform.mystatictext.visible</code> but I was not able to find a way to directly reference the property of an object and change its value with a simple assignment operation. What I found instead is that the way to hide objects is using the <code>ShowWindow()</code> method. I also run into difficulties trying to point this <em>(or any other)</em> method to the STATIC TEXT object, because apparently it has an ID of IDC_STATIC, which, apparently, cannot be referred to, as all static objects have this same ID. To simplify the task ahead, instead of hiding the STATIC TEXT I settled for hiding the button itself, and ended up with this code:</p>
  <pre class="lang-cpp prettyprint-override"><code>void CTutMFC01pDlg::OnBnClickedButton1()
  {
    // TODO: Add your control notification handler code here
    CWnd* pMyButtonObj = GetDlgItem(IDC_BUTTON1);
    pMyButtonObj-&gt;ShowWindow(SW_HIDE);  //or SW_SHOW
  }
  </code></pre>
  <p>This compiles and works very well. Obviously, once the button is pressed and disappears from the window, there is nothing to press again in order to unhide what was hidden. So I tried to move on from this already working code and modify it to act on the STATIC TEXT instead of the button itself. Logic suggested <em>(my logic, anyways)</em> that in order to gain the ability to refer to the ID of the STATIC TEXT, I need to assign a different ID to the STATIC TEXT. Something I can refer to. Something other than the not referrable IDC_STATIC. So I selected the STATIC TEXT object on the Dialog Editor, and in its Property palette I changed the value of the ID property from IDC_STATIC to IDC_STATIC1. This strangely has also changed the NAME property of the object to IDC_STATIC11. Earlier the NAME was IDC_STATIC1. Then in the code of OnBnClickedButton1() I replaced IDC_BUTTON1 with IDC_STATIC1, but that fails to compile complaining that there is no such object. Same happens when tried with IDC_STATIC11.<br />
  A little experimenting revealed another phenomena I am unable to explain <em>(or understand)</em>. Similarly to how I changed the ID of STATIC TEXT, with my button selected in the Property Editor, I changed its ID from IDC_BUTTON1 to IDC_HideBtn. This also changed its NAME property.<br />
  Saved All, rebuilt project, and clicking my button still made it disappear, exactly as it was working before. <strong>HOWEVER</strong>, the source code of OnBnClickedButton1() and the MESSAGE MAP did not get updated to refer to the new ID, IDC_HideBtn, they still refer to IDC_BUTTON1, same as before.</p>
  <pre class="lang-cpp prettyprint-override"><code>void CTutMFC01pDlg::OnBnClickedButton1()
  {
      //TODO: Add your control notification handler code here
      CWnd* pMyButtonObj = GetDlgItem(IDC_BUTTON1);
      pMyButtonObj-&gt;ShowWindow(SW_HIDE);
  }
  </code></pre>
  <p>But at this point, IDC_BUTTON1 should be a non-existing ID. Compile should fail. Yet it compiles fine, and it works fine.</p>
  <p>QUESTIONS:</p>
  <ol>
  <li>Why does the code compile and work with IDC_BUTTON1 in the source while the ID of the button is now IDC_HideBtn?</li>
  <li>What can I do to be able to address the STATIC TEXT item as the argument to GetDlgItem() the same way as I could do with IDC_BUTTON1?</li>
  <li>If STATIC TEXT items are not supposed to be programmatically changed then what other kind of item could I use instead? In Delphi/Lazarus there is a LABEL object similar to STATIC TEXT, but designed to get different Caption or other values many times while the program runs. In the toolbox of the Dialog Editor I see nothing like that, only STATIC TEXT. Or should I use an Input field instead, to display text in the dialog window?</li>
  <li>Is there a way to implement the button click method in the way I initially tried to do the Delphi/Lazarus way? Changing the target object to visible from hidden, and to hidden from visible. Preferrably as a one-liner.</li>
  <li>Is there NO WAY to directly refer to the property of an object and change its value with an assignment operation? Or only I did not find it how?</li>
  </ol>