The edit class is in some ways the simplest predefined window class and in other ways the most complex. When you create a child window using the class name ”edit,“ you define a rectangle based on the x position, y position, width, and height parameters of the CreateWindow call. This rectangle contains editable text. When the child window control has the input focus, you can type text, move the cursor around, select portions of text using either the mouse or the Shift key and a cursor key, delete selected text to the clipboard by pressing Shift-Del, or insert text from the clipboard by pressing Shift-Ins.
One of the simplest uses of edit controls is for single-line entry fields. For instance, the Windows PIF Editor program uses edit controls in this way on its main window. But edit controls are not limited to single lines. For example, the Windows Notepad program uses a multiline edit control. The file size of the Notepad program is surprisingly small_ less than 32 KB. Most of the editing logic is not in Notepad at all; it's in the edit control logic within Windows.
To give you an idea of the power of edit controls, we'll write a ”Notepad clone“ program called POPPAD1. We'll begin the program in this chapter and continue it in Chapters 9 (when we'll add a menu) and 10 (when we'll use dialog boxes to load and save files). POPPAD1 is shown in Figure 6-5.
POPPAD1.MAK
#-----------------------
# POPPAD1.MAK make file
#-----------------------
poppad1.exe : poppad1.obj poppad1.def
link poppad1, /align:16, NUL, /nod slibcew libw, poppad1.def
rc poppad1.exe
poppad1.obj : poppad1.c
cl -c -Gsw -Ow -W2 -Zp poppad1.c
POPPAD1.C
/*-------------------------------------------------------
POPPAD1.C -- Popup Editor Using Child Window Edit Box
(c) Charles Petzold, 1990
-------------------------------------------------------*/
#include <windows.h>
long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
char szAppName[] = "PopPad1" ;
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd ;
MSG msg;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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 = GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
GetSystemMetrics (SM_CXSCREEN) / 2,
GetSystemMetrics (SM_CYSCREEN) / 2,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
static HWND hwndEdit ;
switch (message)
{
case WM_CREATE :
hwndEdit = CreateWindow ("edit", NULL,
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
WS_BORDER | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0,
hwnd, 1,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndEdit) ;
return 0 ;
case WM_SIZE :
MoveWindow (hwndEdit, 0, 0, LOWORD (lParam),
HIWORD (lParam), TRUE) ;
return 0 ;
case WM_COMMAND :
if (wParam == 1 && HIWORD (lParam) == EN_ERRSPACE)
MessageBox (hwnd, "Edit control out of space.",
szAppName, MB_OK | MB_ICONSTOP) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
POPPAD1.DEF
;------------------------------------
; POPPAD1.DEF module definition file
;------------------------------------
NAME POPPAD1
DESCRIPTION 'Popup Editor Version 1 (c) Charles Petzold, 1990'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 8192
EXPORTS WndProc
POPPAD1 is a multiline editor (without any file I/O just yet) in less than 100 lines of C. As you can see, POPPAD1 itself doesn't do very much. The predefined edit control is doing quite a lot. In this form, the program lets you explore what edit controls can do with- out any help from a program.