The scratch window

Date:March 2, 2005 / year-entry #52
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050302-00/?p=36313
Comments:    14
Summary: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...

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index