Home > Software engineering >  Changing the text of the "static text" control and its color at once (a bad behavior occur
Changing the text of the "static text" control and its color at once (a bad behavior occur

Time:11-03

I am trying to change the text of the "static text" control and its color at once, and I have done that, but the problem is when changing the text first and then changing the color of that text, it takes a little noticeable time between changing the text and its color.

bool IsGameOpen = false;
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_INITDIALOG:
    {
        SetTimer(hDlg, GAME_STATUS_TIMER, 1000, NULL);
        return TRUE;
    }
    case WM_TIMER:
        switch (wParam) {
        case GAME_STATUS_TIMER:
            if (Mem->FindProcess()) {
                SetDlgItemTextW(hDlg, GAME_CURRENT_STATUS_LBL, L"Open");
                IsGameOpen = true;

            } else {
                SetDlgItemTextW(hDlg, GAME_CURRENT_STATUS_LBL, L"Closed");
                IsGameOpen = false;
            }
            break;
        }
        return TRUE;
    case WM_CTLCOLORSTATIC:
    {
        if ((HWND)lParam == GetDlgItem(hDlg, GAME_CURRENT_STATUS_LBL)) {
            if (IsGameOpen) {
                SetTextColor((HDC)wParam, RGB(29, 122, 9));
                return (BOOL)GetSysColorBrush(COLOR_MENU);
            } else {
                SetTextColor((HDC)wParam, RGB(176, 12, 12));
                return (BOOL)GetSysColorBrush(COLOR_MENU);
            }
        }
        break;
    }
    }

    return FALSE;
}

What's wrong with my code that makes the program take a little noticeable time between changing the text and its color?

CodePudding user response:

As stated in comments, the Static control is likely being repainted immediately, and thus sending WM_CTLCOLORSTATIC to you, while SetDlgItemTextW() is being processed, but you haven't updated your IsGameOpen variable yet, so the new color doesn't take effect until the next time the Static control has to be repainted.

Try this instead:

case GAME_STATUS_TIMER:
    IsGameOpen = Mem->FindProcess();
    SetDlgItemTextW(hDlg, GAME_CURRENT_STATUS_LBL, IsGameOpen ? L"Open" : L"Closed");
    break;
  • Related