Add a CreateWindow Function

First, you need to retrieve the dimensions of the client area so that you can set the size of the control. Once you have the dimensions of the client area, use the CreateWindow function to create the edit control.

Add the following statements to the WinMain function immediately after creating the main window:

GetClientRect(hWnd, (LPRECT) &Rect);

hEditWnd = CreateWindow("Edit",

NULL,

WS_CHILD | WS_VISIBLE |

ES_MULTILINE |

WS_VSCROLL | WS_HSCROLL |

ES_AUTOHSCROLL | ES_AUTOVSCROLL,

0,

0,

(Rect.right-Rect.left),

(Rect.bottom-Rect.top),

hWnd,

IDC_EDIT,

hInst,

NULL);

if(!hEditWnd) {

DestroyWindow(hWnd);

return (NULL);

}

The GetClientRect function retrieves the dimensions of the the main window's client area and places that information in the Rect structure. The CreateWindow function creates the edit control, using the width and height computed by the Rect structure.

The CreateWindow function creates the edit window. To create an edit control, you need to use the predefined Edit control class and you need to specify the WS_CHILD window style. The predefined controls may be used as child windows only. They cannot be used as main or pop-up windows. Since a child window requires a parent window, the handle of the main window, hWnd, is specified in the function call.

For this edit control, a number of edit-control styles are also specified. Edit-control styles, like window styles, define how the control will look and operate. This edit control is a multiple-line control, meaning the user will be able to enter more than one line of text in the control window. Also, the control will automatically scroll horizontally or vertically if the user types more text than can fit in the window.

The upper-left corner of the edit control is placed at the upper-left corner of the parent window's client area. A child window's coordinates are always relative to the parent window's client area. The next two arguments, Rect.right–Rect.left and Rect.bottom–Rect.top, define the height and width of the edit control, ensuring that the edit control fills the client area when the window is first displayed.

Since an edit control sends notification messages to its parent window, the control must be given a control ID. Child windows cannot have menus, so the menu argument in the CreateWindow function is used to specify the control ID instead. For this edit control, the ID is set to IDC_EDIT. Any notification messages sent to the parent window by the edit control will contain this ID.

If the edit control cannot be created, the CreateWindow function returns NULL. In this case, the application cannot continue, so the DestroyWindow function is used to destroy the main window before terminating the application.