问个《windows程序设计》上的一个例子
初学windows程序设计,看得有点糊了,问个第九章的一个例子:
--------------------------------
/*----------------------------------------
COLORS1.C -- Colors Using Scroll Bars
(c) Charles Petzold, 1998
----------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ScrollProc (HWND, UINT, WPARAM, LPARAM) ;
int idFocus ;
WNDPROC OldScroll[3] ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Colors1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = CreateSolidBrush (0) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Color Scroll"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static COLORREF crPrim[3] = { RGB (255, 0, 0), RGB (0, 255, 0),
RGB (0, 0, 255) } ;
static HBRUSH hBrush[3], hBrushStatic ;
static HWND hwndScroll[3], hwndLabel[3], hwndValue[3], hwndRect ;
static int color[3], cyChar ;
static RECT rcColor ;
static TCHAR * szColorLabel[] = { TEXT ("Red"), TEXT ("Green"),
TEXT ("Blue") } ;
HINSTANCE hInstance ;
int i, cxClient, cyClient ;
TCHAR szBuffer[10] ;
switch (message)
{
case WM_CREATE :
hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
// Create the white-rectangle window against which the
// scroll bars will be positioned. The child window ID is 9.
hwndRect = CreateWindow (TEXT ("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_WHITERECT,
0, 0, 0, 0,
hwnd, (HMENU) 9, hInstance, NULL) ;
for (i = 0 ; i < 3 ; i++)
{
// The three scroll bars have IDs 0, 1, and 2, with
// scroll bar ranges from 0 through 255.
hwndScroll[i] = CreateWindow (TEXT ("scrollbar"), NULL,
WS_CHILD | WS_VISIBLE |
WS_TABSTOP | SBS_VERT,
0, 0, 0, 0,
hwnd, (HMENU) i, hInstance, NULL) ;
SetScrollRange (hwndScroll[i], SB_CTL, 0, 255, FALSE) ;
SetScrollPos (hwndScroll[i], SB_CTL, 0, FALSE) ;
// The three color-name labels have IDs 3, 4, and 5,
// and text strings "Red", "Green", and "Blue".
hwndLabel [i] = CreateWindow (TEXT ("static"), szColorLabel[i],
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0,
hwnd, (HMENU) (i + 3),
hInstance, NULL) ;
// The three color-value text fields have IDs 6, 7,
// and 8, and initial text strings of "0".
hwndValue [i] = CreateWindow (TEXT ("static"), TEXT ("0"),
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0,
hwnd, (HMENU) (i + 6),
hInstance, NULL) ;
//////////////////////////////////////////////////////////////
OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i],
GWL_WNDPROC, (LONG) ScrollProc) ;
hBrush[i] = CreateSolidBrush (crPrim[i]) ;
}
hBrushStatic = CreateSolidBrush (
GetSysColor (COLOR_BTNHIGHLIGHT)) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
return 0 ;
//...
case WM_DESTROY :
DeleteObject ((HBRUSH)
SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)
GetStockObject (WHITE_BRUSH))) ;
for (i = 0 ; i < 3 ; i++)
DeleteObject (hBrush[i]) ;
DeleteObject (hBrushStatic) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
int id = GetWindowLong (hwnd, GWL_ID) ;
switch (message)
{
case WM_KEYDOWN :
if (wParam == VK_TAB)
SetFocus (GetDlgItem (GetParent (hwnd),
(id + (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3)) ;
break ;
case WM_SETFOCUS :
idFocus = id ;
break ;
}
return CallWindowProc (OldScroll[id], hwnd, message, wParam, lParam) ;
}
------------------------------------
最后一个窗口过程ScrollProc,程序里为何没有为其注册窗口类?程序中
OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i], GWL_WNDPROC, (LONG) ScrollProc) ;
这句话代表什么?程序什么时候会调用最后那个窗口过程ScrollProc?