1. Font control of controls Controls can be created in two ways. One is to create a widget in the dialog box module. You can use the dialog box editor to add, select, delete, layout, define, and change the widget size. Another method is to call the createwindow or createwindow Wex function to create a control. You must specify the control window class. In the preceding two methods, the font and size change methods of controls are different. Because the first method is more operational, it is suitable for most programmers. 1.1Control in the dialog box Module The procedure is as follows: In the Properties dialog box, select the general option; Click the font button to bring up the font dialog box; Select the font type (such as) and size (such as the 9th) to change the font of all controls in the dialog box. What needs to be raised here is: what kind of font is closer to the font of the Windows system dialog box? After repeated exploration, the author found that the font is and the size is the best choice for 9. 1.2 Programming Control The control is actually a seed window, so it has many common window attributes, you can also use window management functions such as showwindow and movewindow to display or hide controls, change the location and size of controls, and perform other operations. These window functions are member functions of the cwnd class. In this class, the member functions setfont and getfont are used to set and obtain the font of the window. Their function prototype descriptions are as follows: Void setfont (cfont * pfont, bool bredraw = true ); Parameters: Pfont-a new font. Bredraw -- if true, the cwnd object is repainted. Cfont * getfont () const; Return Value: Returns the cfont pointer containing the current font of this window. You only need to call the getdlgitem function to obtain the cwnd class pointer of a control. You can use the two member functions to change the font of the control. Before using a certain font (non-default font), the application creates a logical font. Both the createfont function and the createfontindirect function can be used to create logical fonts. Although calling the createfont function directly creates a font, It is very troublesome to require 14 parameters. During programming, if the font is set to a certain character in ",, -gb2312, -gb2312,,, you need to set lfcharset to GB2312-CHARSET to make the Set Font valid. The related procedures are as follows:
1 cwnd * pwnd = getdlgitem (idc_user); // gets the cwnd pointer of the control whose ID is IDC-USER 2 CDC * PDC = pwnd-> getdc (); 3 cfont font; 4 logfont stfont; 5 memset (& stfont, 0, sizeof (logfont )); 6 stfont. lfheight = muldiv (12,-PDC-> getdevicecaps (logpixelsy), 72); // set the font height to 12 7 stfont. lfweight = FW-NORMAL; 8 stfont. lfcharset = gb2312_charset; 9 strcpy (stfont. lffacename, ""); // set the font to "". 10 font. createfontindirect (& stfont ); 11 cfont * oldfont = PDC-> SelectObject (& font ); 12 pwnd-> setfont (& font, true ); 13 font. deleteobject (); 14 PDC-> SelectObject (oldfont ); 15 pwnd-> releasedc (PDC );
Refer to the section about clistctrlstyled in the code above to introduce a batch of PDC into ctestingview. oninitialupdate (). The specific code is as follows: 1 2 cwnd * pwnd = getdlgitem (idc_list2); // gets the cwnd pointer of the control whose ID is IDC-USER 3 CDC * PDC = pwnd-> getdc (); 4 cfont font; 5 logfont stfont; 6 memset (& stfont, 0, sizeof (logfont )); 7 stfont. lfheight = muldiv (16, PDC-> getdevicecaps (logpixelsy), 72); // set the font height to 12 8 stfont. lfweight = fw_normal; 9 stfont. lfcharset = gb2312_charset; 10 strcpy (stfont. lffacename, ""); // set the font to "". 11 font. createfontindirect (& stfont ); 12 13 pwnd-> setfont (& font, true ); 14 15 // m_listctrlstyled.setfont (& font); // <= the original use of this statementIn addition, Windows provides six inventory fonts. The application can call the getstockobject function and provide the appropriate identification parameters to obtain the logical font of the inventory. The logo numbers of these six inventory fonts are: system-font, system-fixed-font, ANSI-fixed-font, ANSI-var-font, OEM-fixed-font, deice-default-font (applicable only to Windows NT Operating Systems)) or DEFAULT-GUI-FONT (for Windows 95 operating systems only ). In use, you only need to delete the 4-10 Statement of the above program, and add the following statement to the deleted statement. The font size is automatically determined by the system: Hfont; Hfont = (hfont) getstockobject (OEM-FIXED-FONT ); Font. fromhandle (hfont ); 2 control text color control [3] Before you draw a control, Windows sends a WM-CTLCOLOR message to its parent window to get the color of the control. The cwnd object processes WM-CTLCOLOR messages by resetting the virtual method onctlcolor. If an hbrush is returned, it is used to draw the background of the control. You can set the foreground and background color of the control text before the onctlcolor method is returned to control the control text color [1]. In addition, other functions of Text Formatting, such as setbkmode, settextcharacterextra, and settextalign, are equally valid for control text. The following is an example: 1) create a new dialog box resource IDD-DIALOG1 in a project and add a new class ctestdlg Based on cdialog. 2) use the resource editor to add the list box controls IDC-tlist, static text controls IDC-tstat, test button IDC-tbutt, and exit button idok. 3) Use classwizard to add message processing programs and member variables Add message processing functions for the following objects: |