MFC event handler calls SetWindowText for main dialog textbox. Only 1st and last text appears

242 views Asked by At

My MFC dialog's ON_CHECKED... event handler calls a dialog member "doit" that calls SetWindowText onto a dialog static textbox (cMyStatic). Only the 1st and last Set..text data appears in the textbox. I have tried with cMyStatic.Invalidate() and with UpdateData(). It appears that my SetWindowText is being blocked. The sequential code in that same member AFTER the request for SetWindowText does 2 sequential CreateProcess calls, and those processes run perfectly (they do nothing with the dialog - i.e., no user interaction). There is only ONE dialog, created in VS2019, with System Modal set to FALSE. Clearly, I am missing some understanding of the communication between members called from an ON_ unit and the dialog itself, but I have found no references that make it clear what is wrong or how to properly insert text into a dialog's textbox when an ON_ unit for that dialog is running. Can anyone offer suggestions? I suspect I may need to start an independent thread to do the Set..Text, but before I try do that, I'm hoping someone with nmore experience can give me some guidance.

1

There are 1 answers

12
Constantine Georgiou On

Sounds like the calling/owning thread is busy between the SetWindowText() calls. SetWindowText() doesn't paint the window immediately (check the documentation), the changes will become visible when the WM_PAINT message will be processed, which is low-prioritry message, processed just before the thread is about to enter the idle state. You may just be receiving a single WM_PAINT message as the result of multiple SetWindowText() calls.

Try calling UpdateWindow() (for the static control, and all other controls you want to be updated immediately, or else "synchronously"), just after calling SetWindowText() or UpdateData(FALSE), this bypasses the message-queue and updates the control.

And this has nothing to do with calling these from inside the ON_CHECKED... handler.


EDIT:

I made a small test, but couldn't reproduce your problem. Here is some code:

for (int i = 0; i < 5; i++)
{
    CString s;
    s.Format(_T(" Count: %d"), i+1);
    cMyStatic.SetWindowText(s);
    //cMyStatic.UpdateWindow();
    Sleep(1000);
}

It is called from the one and only thread of the project, the main (UI) thread, which owns all windows. This works, even without calling UpdateWindow()! Same behavior even if I replace Sleep(1000) with a loop calling GetTickCount() until the time elapses. That is, for a static control SetWindowText() updates the control immediately. You mentioned a "static textbox", is this a static control or a disabled edit control? In my test I used a static control:

    LTEXT "",IDC_MYSTATIC,94,34,72,12,0,WS_EX_CLIENTEDGE

Tested with VS2022, Windows SDK Version: 10.0 (latest installed version)

Again, there is no ON_CHECKED... handler, as far as I know.

There must be something wrong with you project, make a copy and start removing stuff, until it is reduced to a MRE...