0

I looked at this question and answer: Changing static text in dialog box at runtime and it looks like what I need EXCEPT I do NOT want all my other variables to be updated onto the main dialog. I am trying to use this text box to show the user what the program is doing (using my own text, sort of like a trace, but without a separate pop-up window). Is there a way of doing this? I already have it working WITH the UPDATEDATA.

DocDJ
  • 31
  • 5

1 Answers1

0

You should avoid calling UpdateData. That is done by the infrastructure in CDialog::OnInitDialog and CDialog::OnOK. What you want to do is create a control member and SetWindowText directly.

Click on your static control in the resource editor. Notice that the ID is IDC_STATIC. You have to change that to something else, like, IDC_MY_STATIC or the rest of this won't work. Now right click on the static control and Add Variable The only thing I've entered is the name of the control, cMyStatic.

enter image description here

Then OK the dialog.

Now in the C++ header of your dialog you will get a member: CStatic cMyStatic;

In your code, anywhere and anytime you would like, you can:

cMyStatic.SetWindowText(L"My Notification")

Anytime you want to have access to a control, create a variable. Examples you find that use GetDlgItem are just wrong. The only place you might used GetDlgItem is in a custom DXX_Special global.

I would add, this works for a lot of stuff. For instance, you want to change the label on a button at run time. Or say you don't want a button enabled until another event.

cMyButton.EnableWindow(FALSE);

In your OnInitDialog. Then when you are ready for the button to work.

cMyButton.EnableWindow();

lakeweb
  • 1,859
  • 2
  • 16
  • 21
  • When I tried to change the control ID to IDC_MY_STATIC and create a variable called m_tracer, I get this error "Failed to return new Code Element. Possibly syntax error. New Element Name: m_tracer. I also tried to create a new static text box and the same thing happened. When I open the Class Wizard, I see there is an Object called IDC_MY_STATIC, but it doesn't appear to be attached to anything and I can't delete it. PS. I am using VS 2019. The only pre-compiled header is pch.h from MFC setup. – DocDJ Apr 06 '23 at 11:11
  • If this is a fairly new project that has not been rearranged, it does not make a lot of sense. Is your `#include "Resource.h"` in the `pch.h`? That will break it. The wizard wants to find it in the cpp file of the dialog. Check that `IDC_MY_STATIC` is in your `resource.h`. At the worst, you can manually add the `CStatic cMyStatic` to the dialog header and in the `DoDataEchange` member, you would add `DDX_Control(pDX, IDC_MY_STATIC, cMyStatic);` This is what the wizard does. But if you can get the wizard to work, that would be better. I've had it fail and manually added my own code in the past. – lakeweb Apr 06 '23 at 15:28
  • #include is inside which is #include'd in mypgmDlg.cpp. I'll try inserting your suggested code by hand and see what happens. Thanks for hanging in there with me. IDC_MY_STATIC does exist in resource.h. – DocDJ Apr 06 '23 at 16:08
  • That is probably the reason. It should be `#include "Resource.h"` right in the cpp of the dialog. `` is not conventional for the wizard, and, I'm somewhat guessing. I'll take it this is an old project that you have inherited and it is in cranky shape for wizards. My main one went back to 6.0 and a few years ago I finally had to make a fresh solution and bring all my stuff in. – lakeweb Apr 06 '23 at 16:23
  • This project got started about 2 months ago. Moving the #include to the main dialog file, caused all the MFC built-in code to be undefined. However, I noticed that "resource" was not capitalized. So I moved the #include back to its original position (where VS project creation put it), changed the "r" to "R", closed & re-opened VS and the wizard now works as does the code you suggested. Thanks again for all your help. I saved your answers in case I run into trouble again. – DocDJ Apr 07 '23 at 11:33
  • The code is working, but there is one strange thing: it is at the top of a loop and I only see the text at the beginning and end of the loop. I tried adding a CMyStatic.invalidate() immediately after the SetWindowText, but that didn't help. Each iteration of the loop takes a varying amount of time, from 1 to 5 minutes, to do its job, so there is plenty of time between SetWindowText calls, to see the text. Any ideas what might be happening? – DocDJ Apr 09 '23 at 11:22
  • You didn't set this job off in its own thread? You can not do work in the main thread that blocks the message pump and expect the dialog to work. [Here is an example](https://stackoverflow.com/questions/63330104/how-to-use-boostbeast-download-a-file-no-blocking-and-with-responses#63512751) of what I did, don't worry about the worker thread, look a the `Main` work which emulates the main of an MFC app. And, I will always prefer the std::thread over a CWinThread. – lakeweb Apr 09 '23 at 23:48
  • I'll look at your example, but here is an outline of my dialog: for ( ){prep loop; SetWindowTexta; CreateProcess} The process is the "job". – DocDJ Apr 10 '23 at 11:21
  • I don't have enough information. Are you doing a `WaitForSingleObject` or setting up pipes and moving on? – lakeweb Apr 10 '23 at 15:35
  • As noted in my outline, I do an explicit WaitForSingleObject. I also tried to add a Sleep() before the wait, which didn't help. My Main() is an MFC modeless (I think) dialog in C++. However, the "work jobs" and the Send..Text are called when a checkbox on my dialog is checked. So everything is happening inside that ON_ member. Since I got the 1st and last Send..Text msgs and everything else works great, I thought there was just a timing problem. Should I try a different mechanism? – DocDJ Apr 10 '23 at 20:15
  • I don't see anything about `WaitForSingleObject` above. But there you have it, you can not block the main thread and expect the dialog to work. There is no way around it. The main thread pumps messages that runs the dialog. I have a couple of dialogs that call console apps. The processes are put in there own thread. You should ask a new question as this one has nothing to do with your present challenge. – lakeweb Apr 10 '23 at 22:47