Part III: MFC style control is a user interface object used to establish a Windows application user interface. Most of the Windows applications and dialogs you have seen are intended by some controls to implement the program function. In order to establish a valid application, you must fully understand how to use it reasonably in a Windows application. There are six basic controls: CSTATIC, CButton, CEDIT, CLIST, CCOMBOBOX and CSCROLLLBAR. In addition, Windows 95 adds 15 enhanced control. What you need to understand is what control can do. How should you control its appearance and behavior and how to make control respond to user events. Just master these, plus the menu and dialog box, you can build any Windows applications you imagined. You can use the program code in this tutorial to build control, or you can use the resource editor through the resource file. Of course, the dialog editor is more convenient, and it is especially useful for the case that has been basically controlled. The simplest control is CSTATIC, which is used to display static text. CSTATIC classes do not have any data members, which only have a small number of member functions: constructor, create function (used to get and set icons on static control), and so on. It does not respond to user events. Because of its simplicity, it is best to use it as the beginning of the WINDOWS control. In this lecture, we start from CSTATIC to see how to modify and customize control. In the next lecture, we will learn CButton and CScrollbar classes to understand the concept of event processing. Once you understand and master all control extremes, you can build a complete application. The CSTATIC class in the foundation MFC is used to display static text information. These information can be used as pure information (eg, an error message displayed in the information dialog), or as a small label or the like. In the Windows Application File Open dialog, you will find six such labels. CSTATIC control has several other display formats. You can make it a rectangle, border or icon, etc. by modifying the style of the label. CSTATIC control is always in the form of a sub-window. Typically, its parent window is an application's main window or dialog. As mentioned above, you can build a static control: 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 MFC to establish all control code. Calling New to assign memory for the CSTATIC class, and then call the structure of the class. The constructor is the initialization function used to complete the class. The CREATE function establishes control and put it on the screen. The Create function has five parameters: lpsztext: Specifies the text to display. RECT: The location, size, and shape of the text area. PParentWnd: Specifies the parent window that CSTATIC control. This control will appear in its parent window, and its location is relative to the user area of its parent window. NID: integer value indicates the identifier of the control. DWStyle: The most important parameter. It controls the appearance and behavior of control. CSTATIC style All controls have various display styles. The style is determined by the DWStyle parameter passed to its DWStyle parameter when establishing a CREATE function. For CSTATIC Effective Style Description is as follows: Pattern inherited from CWnd: WS_CHILD CSTATIC must be. WS_Visible indicates that the control should be visible to the user. WS_DISABLED indicates that the control refuses to accept user events. The text area controlled by WS_Border has a border. CSTATIC Inheitial Style: SS_BLACKFRAME This control area is displayed in a rectangular boundary. Colors are the same as the window frame.
SS_BLACKRECT? This control is displayed in a filling rectangle. Colors are the same as the current window frame. SS_CENTER text home. SS_GRAYFRAME control is displayed in a rectangular border. The color is the same as the current desktop. SS_GRAYRECT This control is displayed in a fill rectangle. The color is the same as the current desktop. SS_ICON control is displayed in the form of an icon. Text as the name of the icon in the resource file. The RECT parameter only controls the location. SS_LEFT text is left. Text can be wrapped around. SS_LEFTNOWORDWRAP text is left. Excess text is tailored. SS_NOPREFIX indicates that "&" characters in the string do not represent an acceleration prefix. SS_RIGHT text is displayed. Text can be wrapped around. SS_SIMPLE is simply displaying a line of text. Any CTLColor information is ignored by its parent window. SS_USERITEM user definition item. SS_WHITEFRAME is displayed in a rectangular border. The color is the same as the current window background. SS_WHITERECT control is displayed in a fill rectangle. The color is the same as the current window background. Among these constants, the "SS" (Static style) can only be used for CSTATIC control. The constant indicates that the "WS" (Window Style) can be applied to all windows, which are defined in the CWND object. There are still a lot of "WS" style constants in CWnd. You can find them in the CWnd :: Create function in the MFC document. The above four are only used for CSTATIC objects. The CSTATIC object is at least two styles: WS_CHILD and WS_VISIBLE. This control must be established as a sub-window of another window. If WS_Visible is not used, the established control is invisible. WS_DISABLED controls the response of the label to the event because the CSTATIC does not receive a keyboard or mouse event, so it is redundant. All other style options are optional, which controls the appearance of the label. Use these controls in the cstatic :: Create function, you can control the display on the screen on the screen. The following code is helpful for understanding CSTATIC. It is similar to the code introduced in the previous lecture, but modifies the establishment of CSTATIC.
//static1.cpp #include // Declare the application class class CTestApp: public CWinApp {public: virtual BOOL InitInstance ();}; // Create an instance of the application class CTestApp TestApp; // Declare the main window class class CTestWindow : public CFrameWnd {CStatic * cs; public: CTestWindow ();}; // The InitInstance function is called // once when the application first executes BOOL CTestApp :: InitInstance () {m_pMainWnd = new CTestWindow (); m_pMainWnd-> ShowWindow (m_nCmdShow); m_pMainWnd-> UpdateWindow (); return TRUE;} // The constructor for the window class CTestWindow :: CTestWindow () {CRect r; // Create the window itself Create (NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRECT (0,0,200,200)); // Get the size of the client rectangle getClientRect (& r); r.inflatecture (-20, -20); // Create a static label cs = new cstatic (); cs-> create ("Hello World", WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, R, THIS);} The following is the window constructor plus the line number: ctestWindow :: C TestWindow () {CRECT R; // Create The Window Itself 1 Create (NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRECT (0, 0, 200, 200)); // Get The Size of The Client Rectangle 2 getClientRect (& R); 3 r .Inflaterect (-20); // Create a static label 4 cs = new cstatic (); 5 cs-> create ("Hello World", WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, R, THIS);} First Call the CTestWindow :: Create function in clicking 1 line. It is the Create function of the cframewnd object because CTestWindow inherits its behavior from CFrameWnd. So the code in the first line specifies that the window size should be 200 × 200 pixels, and the upper left corner of the window is initialized at the 0,0 position of the screen. Constant RectDefault can be replaced with CRECT parameters. On the second line, call CTestWindow :: getClientRect, pass the & R parameter to it. The GetClientRect function is inherited from the CWND class. The variable R is the CRECT type, and the beginning portion of the function is illustrated as a local variable.
There may be two questions when understanding this code 1) What is the getClientRect function? 2) What is the CRECT variable? Let us answer the first question first. When you view the CWnd :: getClientRect function in the MFC document, you will find it to return a CRECT type that contains the user area rectangle of the specified window. It saves the address & r of the parameters & r. This address points to the location of CRECT. The CRECT type is defined in the MFC. It is very convenient to use it to handle rectangles. If you look at the following MFC documents, you will see a member function and operator that defines more than 30 processing rectangles. In our case, we have to display "Hello World" in the middle of the window. Therefore, we use getClientRect to get the rectangular coordinates of the user area. Creat :: Inflatrect is called in line 3, while also increases or decreases the size of the rectangle (see CRECT:: DeflateRect). Here we decrease 20 pixels each side of the rectangle. If this is not this, the boundaries around the label will exceed the window frame. In fact, CSTATIC is established in lines 4 and 5. Styles properties are centered and have borders. The size and position are determined by the CRECT parameter R. By modifying different style properties, you can understand different forms of CSTATIC. For example, the following code contains modifications to the CTESTWINDOW constructor, and the control has a displacement: ctestWindow :: CtestWindow () {create r; // Create The window Itself Create (Null, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRECT (0,0,200,200)); // Get the size of the client rectangle getClientRect (& r); r.inflatecture (-20, -20); // Create a static label cs = new cstatic (); cs-> create ("Now Is The Time for All Good Men TO / COME TO The AID OR Country", WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, R, THIS);} There is no difference in the above code except that the text is compared. You can see if you run the code, CSTATIC has wrapped around the text in the specified area, and there is no hide. If the border rectangle is too small to contain all text rows, the text will be cut to accommodate. You can see this feature of the CSTATIC if you reduce the size of the rectangle or increase the string length. In all the code we see, style ss_center is used in Chinese. CSTATIC also allows left alignment or right alignment. The left alignment is used to replace the SS_Center property with SS_LEFT. Similarly, right or right is to replace it with SS_RIGHT. The SS_LEFTNOWORDWRAP property is used to close the text round. It will force the use of left alignment properties. CSTATIC's rectangular display mode CSTATIC also supports two different rectangular display modes: fill rectangles and frames. It is usually used in these two modes to bring together a set of controls. For example, you can put black background frame windows as a set of edit boxes. You can choose six different styles: SS_BLACKFRAME, SS_BLACKRECT, SS_GRAYFRAME, SS_GRAYRECT, SS_WHITEFRAME, and SS_WHITERECT. RECT forms a filled rectangle, while the frame consists of one side. The color logo, such as SS_WHITERECT indicates that its color is the same as the color of the window background. Although the default value of this color is white, you can use the control panel to change, at this time, the color of the rectangle may not be white.
When a rectangle or frame properties are specified, the text string of CSTATIC will be ignored. Typical situations are transmitted. You can test these features. Font You can use the CFont class to change the font of the CSTATIC. The CFont class in the MFC holds a single instance of a special Windows font. For example, an instance of the CFont class may save 18 points of Times fonts, while the other may save 10 points Courier fonts. You can call the setFont function to modify the font. The following code gives how to implement fonts. CTestWindow :: CTestWindow () {CRect r; // Create the window itself Create (NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRect (0,0,200,200)); // Get the size of the client rectangle GetClientRect (& r); r .Inflatecture (-20, -20); // Create a static label cs = new cStatic (); cs-> create ("Hello World", WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, R, THIS); // Create A new 36 point Arial font font = new CFont; font-> CreateFont (36,0,0,0,700,0,0,0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "arial"); // Cause the Label to Use the New font cs-> setfont (font);} The above code begins with the creation window and cStatic. Then create a CFont type object. The font variable should be "cfont * font" as a data member of CTESTWINDOW. The cfont :: CreateFont function has 15 parameters, but only three are most common. For example, 36 specifies the font size of the point unit, 700 specifies the density of the font (400 is normal "normal", 700 is the black "bold", the value of the value is 1 to 1000. Fw_normal and fw_bold meanings actually Is the same), "arial" is the name of the font used. Windows usually has five TRUE TYPE fonts (Arial, Courier New, Symbol, Times New Roman, and Wingdings), you can make sure that there will be this font on any machine. If you use the font that you don't know, CFont will choose the default font, just as you can see in this tutorial. To learn more about the CFont class, see the MFC documentation. In the API online help file, there is a good overview of the font. Find "Fonts and Text Overview". The setFont function is inherited from CWND. It is used to set the font of the window, in our program is a CSTATIC sub-window. You may have to ask: "How do I know which functions in CWnd can be used for CSTATIC?" You can only learn in practice. Take some time to look at all functions of CWND. You will have something to gain and you will find which functions can be used to customize control. We select other SET functions in the CWND class in the election. Conclusion In this tutorial, we surveted a lot of different characteristics in CSTATIC. For the SET function inherited from CWND, we will put it in the next lecture because it is more appropriate.