March 5th, 2015

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

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.