Edit Control in Windows
Windows Predefined Controls – Part 3
Volume - Windows User Interface
Forward: In this part of the series, we look at the Edit Control in general terms.
By: Chrysanthus Date Published: 29 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
An Edit Control
An edit control is a rectangular child window that enables a user to enter and edit text for an application.
You can select an edit control by clicking the mouse pointer in it or by pressing the Tab key on the keyboard until the edit control receives focus. When an edit control is selected, it displays a blinking caret that indicates the insertion point of the next character to be typed on the keyboard.
An edit control sends notification codes to its parent window in the form of WM_COMMAND messages. An application can send a message to an edit control using the SendMessage function. We saw the WM_COMMAND message format and the SendMessage function in the previous part of the series.
Type of Edit Control
There are two types of edit controls: the single-line edit control and the multi-line edit control. The single-line edit control would take only one line of text. The multi-line edit control can take many lines of text, with the next line below the current line.
You can create an edit control using the CreateWindowEx function. The following lines will create and display an edit control in a parent window.
HWND hwndEdit1 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD, 100, 100, 150, 15, hwndMain, (HMENU)1, hinstance, NULL);
ShowWindow(hwndEdit1, SW_SHOW);
UpdateWindow(hwndEdit1);
The CreateWindowEx function creates the control and returns a handle to the control, just as it would return a handle to a window created. When this is done, the window is not displayed. The next statement uses the handle of the control to display the control. The last statement (above) uses the handle of the control to paint (give it color) the control’s client area. For the above code segment, the default edit color of white will be used to paint the edit control.
Let us now look at the CreateWindowEx function for the edit control. The first argument can be zero. Since the edit control is of the EDIT class, the second argument is the class, EDIT, in double quotes. If you want text to initially appear in the edit control when it is first displayed, then type the text in double quotes as the third argument, otherwise let the third argument be NULL. The third argument is officially for the name of the control (window).
The fourth argument deals with the styles for the edit control. Since edit control is a child window, you need the windows’ WS_CHILD style at this position. You can combine this style with edit control styles, separating the style identifiers (constants) in the argument with the | operator. We shall see edit control styles in the next part of the series, One of the edit control styles is ES_MULTILINE, which is used when to want the edit control to be a multi-line edit control. So, if you want the above edit control to be multi-line, you would type the following as the fourth argument:
WS_CHILD|ES_MULTILINE
In the absence of the ES_MULTILINE style in the fourth argument of the CreateWindowEx function, a single-line edit control is created.
The tenth argument is the edit control’s (child window’s) identifier. This is an integer that is unique for all child windows (not just edit controls) in a parent window. The data type for identifier in the CreateWindowEx function is HMENU and not an integer. So the integer (type) placed at this position has to be cast (converted) to an HMENU data type by preceding the integer with the word, HMENU in parenthesis.
The eleventh argument is the handle to the instance of the registered class of the application main window. This can be read from the HINSTANCE hinstance parameter of the WinMain entry point function. The WinMain function can determine the instance handle automatically.
If your aim is just to create an edit control (child window), then set the last argument of the CreateWindowEx function to NULL.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT