Programming Control of Chinese characters and colors of VC ++ controls

Source: Internet
Author: User

Programming Control of Chinese characters and colors of VC ++ controls

Ding youhe and Sun Jian

  AbstractThis article focuses on how to use programming methods to control the font and color of Chinese characters and text commonly used controls in the dialog box, take VC ++ 5.0 [1] as an example to provide specific operation methods and related program code.

  KeywordsControls Chinese characters, fonts, colors, and programming

In Windows applications, VC ++ 5.0 provides programmers with various controls to implement an intuitive, convenient, and fast interactive interface, these controls include static controls, buttons, scroll bars, and other general controls widely used in Windows (as shown in Table 1) and Windows 95 and Windows NT (after 4.0) provides the dynamic Connection Library commctrl. the public controls in DLL and comctl32.dll and ActiveX controls that support the Internet are in three categories.
As shown in table 1, almost all controls have text. In the design of the user interface, you often need to change the font and color of the text, and be able to control it in the program. For general users, the text is better to be Chinese characters. Although VC ++ 5.0 MFC supports Chinese characters, the Chinese characters in the control are not easy to display and the programming control of Chinese characters is more difficult. Therefore, the author uses the control type in table 1 to explore how to change the font and color of Chinese characters and text, and uses VC ++ 5.0 as an example to provide specific operation methods and related program code.

Table 1 general control types [2]

Control Type Window class name Text? Description
Static controls Static Yes Used to display users with almost fixed text or graphic descriptions
Button Button Yes Used to generate some commands or change some option settings
Scroll bar Scrollbar None You can change some numeric options by moving the scroll block on the scroll bar.
List box ListBox Yes Displays a list that allows you to select one or more items.
Edit box Edit Yes Allows you to view and edit text in two directions.
Combo box ComboBox Yes It organically combines the list box and the editing box.
Select an existing item in the list and edit the new item.

 

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 statement

In 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:

Object ID Function Message
Ctestdlg Oninitdialog WM-INITDIALOG
Ctestdlg Onctlcolor WM-CTLCOLOR
IDC-TBUTT Ontbutt BN-CLICKED

Switch to the member varble label in the Class Wizard and add the following member variables:

Control ID Type Member
IDC-TLIST Clistbox M-list
IDC-TSTAT Cstring M-Text

4) add member variables to the class ctestdlg
Use the Add variable dialog box to add the following member variables:

Type Statement Access
Colorref M-bkcolor Protected
Colorref M-focolor Protected

5) write the above Code
Void ctestdlg: ontbutt ()
{
M-bkcolor = RGB (0,255, 0 );
M-focolor = RGB (255, 0, 0 );
M-list.Invalidate ();
}
Bool ctestdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
M-bkcolor = getsyscolor (color-window );
M-focolor = getsyscolor (COLOR-WINDOWTEXT );
Tchar szpath [MAX-PATH] = {"C: // windows "};
Dlgdirlist (szpath, IDC-TLIST, IDC-TSTAT, DDL-READWRITE | DDL-READONLY |
DDL-HIDDEN | DDL-SYSTEM | DDL-ARCHIVE); // initializes the IDC-TLIST to a file list
Return true;
}
Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
If (nctlcolor = CTLCOLOR-LISTBOX ){
PDC-> setbkcolor (m-bkcolor );
PDC-> settextcolor (m-focolor );
}
Return HBr;
In the code of the onctlcolor method above (nctlcolor = CTLCOLOR-LISTBOX), the color of the list box class is changed; if there are two lists at the same time, the color of the two lists will change at the same time, instead of changing the color of one of the list boxes separately, There are buttons (CTLCOLOR-BTN), dialogs (CTLCOLOR-DLG), edit boxes (CTLCOLOR-EDIT), message boxes (CTLCOLOR-MSGBOX), scroll bars (TLCOLOR-SCROLLBAR) static text (CTLCOLOR-STATIC) and other controls have similar phenomena.
So far, we can control the font and color of the control text in the program to make the development user interface more beautiful and friendly.

Ding youheMaster students. He is engaged in research on CAD and CAI.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.