The scratch window


Sometimes you need a quick and dirty window and you don't want to go through all the hassle of registering a class for it. For example, you might need a window to do a brief snippet of DDE, or you just need a window to own a message box.

To save yourself the trouble of registering a class for every single weenie thing you might need a window for, you can get lazy and register a single "scratch window" class and simply subclass it on an as-needed basis.

ATOM RegisterScratchWindowClass(void)
{
  WNDCLASS wc = {
        0,                              // style
        DefWindowProc,                  // lpfnWndProc
        0,                              // cbClsExtra
        0,                              // cbWndExtra
        g_hinst,                        // this file's HINSTANCE
        NULL,                           // hIcon
        LoadCursor(NULL, IDC_ARROW),    // hCursor
        (HBRUSH)(COLOR_BTNFACE+1),      // hbrBackground
        NULL,                           // lpszMenuName
        TEXT("Scratch"),                // lpszClassName
  };

  return RegisterClass(&wc);
}

HWND
CreateScratchWindow(HWND hwndParent, WNDPROC wp)
{
  HWND hwnd;
  hwnd = CreateWindow(TEXT("Scratch"), NULL,
                      hwndParent ? WS_CHILD : WS_OVERLAPPED,
	              0, 0, 0, 0, hwndParent, NULL, NULL, NULL);
  if (hwnd) {
    SubclassWindow(hwnd, wp);
  }
  return hwnd;
}

Now if you need a quick one-off window, you can just create a scratch window instead of creating a custom window class just to handle that specific task.

We'll see the scratch window in action soon.

Comments (14)
  1. G. Man says:

    Why not just AfxRegisterWndClass(0)

  2. G.Man – because that’s MFC, not raw Win32?

  3. Waleri says:

    WC_DIALOG is also handy, although one should handle WM_ERASEBKGND or assign a dlgproc manually.

    I always wondered why there is no preregistered generic window class…

  4. Cooney says:

    I always wondered why there is no preregistered generic window class…

    The original windows environment ran on a 1MB real mode machine. That may be why.

  5. A says:

    What’s wrong with using "STATIC" for your scratch windows?

  6. Ivo says:

    I vaguely remember having problems with using STATIC for that purpose. I think they weren’t getting the mouse messages or something. But I may be mistaken…

  7. Igor Tandetnik says:

    Static controls with SS_NOTIFY style do get mouse messages

  8. Jon Potter says:

    But not if you subclass them and don’t pass through to the original wndproc, shirley?

  9. A says:

    I think they weren’t getting the mouse messages or something.

    Why would a scratch window need to receive mouse messages? Unless I’m missing something, the "scratch windows" Raymond is talking about aren’t even shown on the screen.

  10. Raymond Chen says:

    Sometimes scratch windows are displayed on the screen.

  11. Adrian says:

    For those trying out the examples, be sure you change the window class name. The main window in the Raymond’s Scratch program is already using the class name "Scratch".

  12. hmmmmmm says:

    where can i find the function SubclassWindow?

  13. Adrian says:

    hmmmmmm: SubclassWindow (in this context) is a convenience macro in <windowsx.h>.

  14. Keeping temporary state in a per-thread location without using TLS.

Comments are closed.

Skip to main content