Home

The Static Text Control

 

Introduction to the Static Control

A static control is an object that displays information to the user without his or her direct intervention. A static control can be used to display text, a geometric shape, or a picture such as an icon, a bitmap, or an animation. Normally, a user cannot change the value of a static control. For example, if a static control displays text, the user cannot directly change it. In the same way, if a static control is used to show a picture, the user cannot inherently change the picture.



Characteristics of a Static Control

To create a static control, the MFC library provides the CStatic class. Like any other control, there are various ways you can create this control. During design, you can add a Picture control to a form or a dialog box:

The Picture control is the most classic static control of the MFC library. To exploit a static control, you can manipulate some of its characteristics. These characteristics are referred to as its style. The styles applicable on a static control are:

Colored border: You can surround the borders of the control with a black, a gray or a white color. During design, specify the Type as Frame and set the color accordingly using the Color combo box. These characteristics are set as SS_BLACKFRAME, SS_GRAYFRAME, and SS_WHITEFRAME respectively.

The control is filled with either a black, a gray, or a white color. These characteristics are based on the SS_BLACKRECT, SS_GRAYRECT, and SS_WHITERECT values respectively. At design time, set the Type combo box to Rectangle and select one of these colors in the Color combo box.

A static control can also be used to display a bitmap. To make this possible, at design time, set the Type to Bitmap and, in the Image combo box, select a bitmap. These properties are controlled through the SS_ICON or SS_BITMAP

If you want to manipulate the properties of a static control, you should change its identifier from IDC_STATIC to a more meaningful name.

The Static Text Control

The Static Text is a control used to display text to the user without his or her being able to change it. The programmer can change the text of this control but the user can only read it. The Static Text control can be used to create labels which are short text that accompany other controls to indicate what they are used for.

To add static text to a form or dialog box at design time, on the Controls toolbox, click the Static Text button and click in the desired area. If you plan to access the control in your code, you must change its ID to something else than IDC_STATIC. For example, you can identify it as IDC_LABEL

You should also add a variable for the control. If you plan to access its properties as a control derived from CWnd, you should add a CStatic Control Variable for it and you can call it m_Label for example. If you are more interested in the string stored in the control, you should Add a Value Variable for it. Remember that you can still add both types of variables if necessary.

You can also programmatically create a label by declaring a variable or a pointer to CStatic using its constructor. To initialize it, call its Create() method. Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CStatic *Label = new CStatic;

	Label->Create("Saint Lucia", WS_CHILD | WS_VISIBLE,
		          CRect(10, 40, 160, 60), this, 0x188);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

Probably the most important property of a label is the text it displays. This is set at design time by changing the value of the Caption field. If you want to change the caption of the control, you can call the CWnd::SetWindowText() method. Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_Label.SetWindowText("Frederick Randt");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

a static text control displays its caption using the system dialog box, which could be MS Sans Serif or MS Shell Dlg depending on your programming environment. Such a font is applied to all controls on a dialog box or a form. If you want to use a different font for your static control, you must first create the font, then select it for the static control by calling the CWnd::SetFont() method. Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CFont *m_Font1 = new CFont;

	m_Font1->CreatePointFont(160, "Garamond");
	m_Label.SetFont(m_Font1);
	m_Label.SetWindowText("Frederick Randt");

	CStatic *Label = new CStatic;

	Label->Create("Saint Lucia", WS_CHILD | WS_VISIBLE,
		          CRect(10, 40, 200, 100), this, 0x188);

	CFont *m_Font2 = new CFont;
	m_Font2->CreatePointFont(240, "Verdana");
	Label->SetFont(m_Font2);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
} 

To display its caption, the static control starts on the top-left corner of the allocated rectangle. To control the horizontal alignment of text, select the desired value from the Align Text combo box.

A static control can also have its text changed in response to user's action.
 

Copyright © 2003-2005 FunctionX, Inc.