How to Create a Multiline Edit Control
This topic demonstrates how to implement a simple word processor by adding a multiline edit control to the client area of a window. By using the multiline edit control, the user can select edit commands from a menu. These commands enable the user to perform simple editing operations such as undo a previous action, cut or copy selections to the clipboard, paste text from the clipboard, and delete the current selection.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Your application must include code to create an instance of and initialize a multiline edit control and then process user edit commands.
The following C++ code example implements much of the functionality of a simple word processor by adding a multiline edit control to the client area of a window. The system automatically performs wordwrap operations for the edit control and also handles the processing for the vertical scroll bar (created by specifying ES_AUTOVSCROLL in the call to the CreateWindow function).
User edit commands are sent to the window process via WM_COMMAND notification messages.
Note
If the window includes the Windows Ribbon, the size of the edit control must be adjusted to accommodate the height of the Ribbon. For more information, see Windows Ribbon Framework.
#define ID_EDITCHILD 100
LRESULT CALLBACK MainWndProc(HWND hwnd, // window handle
UINT message, // type of message
WPARAM wParam, // additional information
LPARAM lParam) // additional information
{
static HWND hwndEdit;
TCHAR lpszLatin[] = L"Lorem ipsum dolor sit amet, consectetur "
L"adipisicing elit, sed do eiusmod tempor "
L"incididunt ut labore et dolore magna "
L"aliqua. Ut enim ad minim veniam, quis "
L"nostrud exercitation ullamco laboris nisi "
L"ut aliquip ex ea commodo consequat. Duis "
L"aute irure dolor in reprehenderit in "
L"voluptate velit esse cillum dolore eu "
L"fugiat nulla pariatur. Excepteur sint "
L"occaecat cupidatat non proident, sunt "
L"in culpa qui officia deserunt mollit "
L"anim id est laborum.";
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindowEx(
0, L"EDIT", // predefined class
NULL, // no window title
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 0, 0, // set size in WM_SIZE message
hwnd, // parent window
(HMENU) ID_EDITCHILD, // edit control ID
(HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL); // pointer not needed
// Add text to the window.
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) lpszLatin);
return 0;
case WM_COMMAND:
switch (wParam)
{
case IDM_EDUNDO:
// Send WM_UNDO only if there is something to be undone.
if (SendMessage(hwndEdit, EM_CANUNDO, 0, 0))
SendMessage(hwndEdit, WM_UNDO, 0, 0);
else
{
MessageBox(hwndEdit,
L"Nothing to undo.",
L"Undo notification",
MB_OK);
}
break;
case IDM_EDCUT:
SendMessage(hwndEdit, WM_CUT, 0, 0);
break;
case IDM_EDCOPY:
SendMessage(hwndEdit, WM_COPY, 0, 0);
break;
case IDM_EDPASTE:
SendMessage(hwndEdit, WM_PASTE, 0, 0);
break;
case IDM_EDDEL:
SendMessage(hwndEdit, WM_CLEAR, 0, 0);
break;
case IDM_ABOUT:
DialogBox(hInst, // current instance
L"AboutBox", // resource to use
hwnd, // parent handle
(DLGPROC) About);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_SETFOCUS:
SetFocus(hwndEdit);
return 0;
case WM_SIZE:
// Make the edit control the size of the window's client area.
MoveWindow(hwndEdit,
0, 0, // starting x- and y-coordinates
LOWORD(lParam), // width of client area
HIWORD(lParam), // height of client area
TRUE); // repaint window
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return NULL;
}
Related topics