Part Three: MFC style
Control is the user interface object that is used to establish the Windows application user interface. Most of the Windows applications and dialogs that you see are just some of the things that are made up of control to implement the program functionality. In order to build an effective application, you must fully understand how control should be used reasonably in Windows applications. There are six basic controls: CStatic, CButton, CEdit, CList, CComboBox and CScrollBar. In addition, Windows 95 adds 15 enhanced control. You need to understand what that control can do, how you should control its appearance and behavior, and how to let control respond to user events. With this in mind, and with the menu and dialog boxes, you can build any Windows applications you imagine. You can use the program code to build control like this tutorial, or you can use the resource editor to build it through a resource file. Of course, the dialog editor is more convenient, and it is especially useful for situations where control is already in the ground.
The simplest control is cstatic, which is used to display static text. The CStatic class does not have any data members, it has only a small number of member functions: constructors, create functions (for getting and setting icons on static control), and so on. It does not respond to user events. Because of its simplicity, it is best to take it as the beginning of learning Windows control.
In this talk, we start with cstatic, and look at how to modify and customize control. In the next lecture, we will study the CButton and CScrollBar classes to understand the concept of event handling. Once you understand and master all control-critical classes, you can build a complete application.
Basis
The CStatic class in MFC is used to display static text information. This information can be used as pure information (for example, an error message displayed in an Information dialog box), or as a small label. In the Windows Application File Open dialog box, you will notice that there are six of these tags.
There are several other display formats for CStatic control. You can change the style of the tag to make it appear as a rectangle, a border, or an icon.
CStatic control is always present as a child window. Typically, its parent window is the main window or dialog box for the application. As explained in the previous lecture, you can create a static control with two lines of code:
CStatic *cs;
...
CS = new CStatic ();
Cs->create ("Hello World"),
ws_child| ws_visible| Ss_center,
CRect (50,80, 150, 150),
this);
These two lines of code are typical of MFC to build all the control code. Call new to allocate memory for the case of the CStatic class, and then call the constructor of the class. Constructors are used to complete the initialization functionality required by a class. The CREATE function builds control and puts it on the screen.
The CREATE function has five parameters:
Lpsztext: Specifies the text to display.
Rect: Controls the position, size, and shape of the text area.
pParentWnd: Indicates the parent window of the CStatic control. The control appears in its parent window, and its location is relative to the user area of its parent window.
NID: An integer value that represents the identifier for the control.
Dwstyle: The most important parameter. It controls the appearance and behavior of the control.
CStatic Style
All controls have a variety of display styles. A style is determined by the dwstyle parameter that is passed to it when the control is created with the CREATE function. An introduction to CStatic's effective style is as follows: