March 2nd, 2005

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.

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.