0

I have an application, a game, that I've been working on and I stuck with a button not been (visually) updated.

The button is a pause button. When the app starts it is disabled (has WS_DISABLED style), but when the user starts a game, I simply remove that style responsable to disable it (WS_DISABLED).

The problem is: the button remains (visually) with the disabled style (when removing the style).
Or remains (visually) with the enable style (when adding the style).

However, the button is correctly updated when I click in it. I assume that this is a repaint/update issue.

I tried to repaint the window (no sucess):

RedrawWindow(hWnd,NULL,NULL,RDW_ALLCHILDREN | RDW_UPDATENOW);

Here is the code fragment located in WinProc function:

switch (message) {
case WM_CREATE:
    CreateControls(hWnd);
    break;
case WM_COMMAND:
{
    switch (HIWORD(wParam)) {
    case BN_CLICKED:
        switch (LOWORD(wParam)) {
        case 3:
        {
            char text[50];
            GetDlgItemTextA(hWnd, 3, text, 50);
            HWND pauseB = GetDlgItem(hWnd, 4);
            LONG style = GetWindowLong(pauseB, GWL_STYLE);
            if (strncmp(text, "Start", strlen(text)) == 0) {
                SetDlgItemTextA(hWnd, 3, "Stop");
                SetWindowLong(pauseB, GWL_STYLE, style & ~WS_DISABLED);
                std::thread bt(RunGame, hWnd);
                bt.detach();
            } else {
                SetDlgItemTextA(hWnd, 3, "Start");
                SetWindowLong(pauseB,GWL_STYLE,style | WS_DISABLED);
            }
            RedrawWindow(hWnd,NULL,NULL,RDW_ALLCHILDREN | RDW_UPDATENOW);
            //SendMessage(hWnd, WM_PAINT, NULL, NULL);
        }
        default:
            break;
        }
        break;
    default:
        break;
    }
}
break;
// other cases...
}

I may confess that I don't know much about c++. So sorry for my mistakes.

  • Are you programming for a 64 bit computer? Looks like `SetWindowLong` has been superseded by `SetWindowLongPtr`, seen here: https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowlonga Also in that link, "Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function." Perhaps the Disabled state you are activating needs a call to SetWindowPos to take effect. So you could set the window's position to its current position, and then redraw the window, perhaps. – okovko Oct 17 '18 at 04:30
  • Please stop using fixed character arrays already and instead use *C++ strings* and use `LPSTR(some_str.c_str())` inside the parameters to convert it to a C String... Remember that fixed arrays can never have elements more than their size and are prone to throw an exception... – Ruks Oct 17 '18 at 04:31
  • You should also check the return value of SetWindowLong to make sure the function did what you expect. If it fails, it returns zero. If it succeeds, it returns the old value of the style. – okovko Oct 17 '18 at 04:36
  • "*The button is a pause button. When the app starts it is disabled (has WS_DISABLED style), but when the user starts a game, I simply remove that style responsable to disable it (WS_DISABLED).*": Ever heard of the function called [`EnableWindow()`](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enablewindow)? – Ruks Oct 17 '18 at 04:40
  • @Ruks I read in some site that I should avoid using string, because it may cause compatibility issues. (Don't know if that is true but I done is anyway) – Vinícius Gabriel Oct 17 '18 at 04:47
  • @Ruks EnableWindow() for some reason, worked – Vinícius Gabriel Oct 17 '18 at 04:47
  • @ViniciusGabriel It is specifically made for that purpose only... Also, I am against abusing the styles of a **HWND** because it can cause a variety of unknown errors... – Ruks Oct 17 '18 at 04:48
  • 1
    @Ruks fixed character arrays are guilty of many things, but throwing an exception is not one of them :) – Jeremy Friesner Oct 17 '18 at 04:54
  • @JeremyFriesner Yes, they crash, with buffer overflow... – Ruks Oct 17 '18 at 04:56
  • @Ruks Even crashing cannot be granted. That's why the coined that mysterious ["Undefined Behavior"](https://stackoverflow.com/a/4105123/1505939) term. ;-) – Scheff's Cat Oct 17 '18 at 06:24
  • @Ruks crashing is not the same thing as throwing an exception (and I've seen plenty of instances where a crash didn't happen either -- rather data just gets written to memory outside the bounds of the array, sometimes overwriting something important and causing noticeable misbehavior, sometimes not, in a very hard-to-predict manner) – Jeremy Friesner Oct 17 '18 at 16:56

0 Answers0