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)
Comments are closed. |
Why not just AfxRegisterWndClass(0)
G.Man – because that’s MFC, not raw Win32?
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…
What’s wrong with using "STATIC" for your scratch windows?
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…
Static controls with SS_NOTIFY style do get mouse messages
But not if you subclass them and don’t pass through to the original wndproc, shirley?
Sometimes scratch windows are displayed on the screen.
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".
where can i find the function SubclassWindow?
hmmmmmm: SubclassWindow (in this context) is a convenience macro in <windowsx.h>.
Keeping temporary state in a per-thread location without using TLS.