Modifying the CS_NOCLOSE style does affect all windows of the class, just not necessarily in an immediately noticeable way

Raymond Chen

In a discussion of how not to disable the Close button, Rick C claims that changing the style does not affect windows that are already created.

Actually, it does. You can’t see it, but the effect is there.

Take our scratch program and make these changes:

DWORD CALLBACK NewThread(void *)
{
  CreateWindow(
      TEXT("Scratch"),
      TEXT("Scratch 2"),
      WS_VISIBLE | WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT,
      CW_USEDEFAULT, CW_USEDEFAULT,
      NULL, NULL, g_hinst, 0);

  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return 0;
}

void OnChar(HWND hwnd, TCHAR ch, int cRepeat)
{
  DWORD id;

  switch (ch) {
  case ' ':
    SetClassLong(hwnd, GCL_STYLE,
                 GetClassLong(hwnd, GCL_STYLE) ^ CS_NOCLOSE);
    break;

  case '+':
    CloseHandle(CreateThread(0, 0, NewThread, 0, 0, &id));
    break;
  }
}

  HANDLE_MSG(hwnd, WM_CHAR, OnChar);

Run this program, hit the + to open another window, then hit the space bar to set the CS_NOCLOSE style.

The window that is passed to Set­Class­Long updates its close button, but the other window does not.

But this is purely a visual artifact. If you try to click on the close button of either window, it will not work.

So don’t change the CS_NO­CLOSE style thinking that it affects just your window. It actually affects all windows of the class. But it may not look that way at a casual glance.

0 comments

Discussion is closed.

Feedback usabilla icon