Windows Controls: Static Text


Introduction to the Static Text Control



A label is a static control that serves as a guide to the user. It displays text that the user cannot change but can read to get information about a message from the programmer or an indication about another control on the dialog box or form. Most controls on a dialog box are not explicit at first glance and the user would not know what they are used for. Therefore, you can assign a label to the control as a help to the user.


Creating a Static Text Control

The static text control is available in the Toolbox by a control of that name. Therefore, to get a label, click the Static Text button Static Text and click the dialog box. To support the static text, the MFC provides the CStatic class. Therefore, to programmatically create a label, declare a variable of type CStatic and call its Create() method. Here is an example:

BOOL CExerciseDlg::OnInitDialog()

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	CStatic *lblPresent = new CStatic;
	lblPresent->Create(L"Present", WS_CHILD | WS_VISIBLE,
			   CRect(20, 20, 100, 40), this);

	return TRUE;  // return TRUE  unless you set the focus to a control

Characteristics of a Static Text



A user cannot directly change any aspect of a label. For this reason, you will usually not be concerned with a label's IDentifier. If, for any reason, you want to refer to a label in your code, you must first change its identifier from IDC_STATIC to something else.

An Access Key

The most important aspect of a label is the text it displays. This is the Caption property. If the label is used to identify another control, you can help the user access that control using an indicator called an access key. An access key is a underlined letter on the label so that, when clicked, it gives access to the accompanying control.

To create an access key, choose a letter on the label and precede it with the ampersand character &. For example L&etter would produce Letter. When there are many access keys on a form or dialog box, you should make sure that no two access keys are the same. That is, you should not use the same letter on various labels, although this is allowed. Because you can forget to follow this rule, after creating the access keys, you can ask Visual Studio to check for access key duplicates. To do this, right-click the form or dialog box and click Check Mnemonics:

Check Mnemonics

If there are duplicate access keys, you would receive a message accordingly:

Check Mnemonics

If possible, you should correct by changing one of the access keys without using one that exists already.

To use the access keys, the user presses Alt and the letter that corresponds to the access key.

Text Alignment

The text of the caption follows a line alignment that can assume one of three values: Left, Center, or Right. By default, text of a label is aligned to the left. At design time, to change it, select the desired value using the Align Text combo box in the Properties window.


Home Copyright © 2010-2016, FunctionX