VCL Controls: The Static Text Control

 

Introduction

The label control we used earlier is a completely native VCL object and not particularly related to Win32. The Win32 library provides its own support to labeled text through a window class named STATIC. To support the Win32 STATIC control, the VCL provides the StaticText control. Over all, the VCL’s Label and StaticText controls are very similar. The biggest difference is that StaticText is a Windows control, like the STATIC class of the Win32 API while the VCL’s Label control is not.

To create a static text, at design time, from the Additional tab of the Component Palette, you can click the StaticText button and click the container that would host it.

Characteristics of the Static Text Control

As stated already, the static text provides the same functionality as the label control. Its text is aligned using the Alignment property. It also has the following properties similar to those of the label: AutoSize, FocusControl, and ShowAccelChar.

Among the differences between both controls, the StaticText does not have a Canvas property because you cannot draw into it. The StaticText control has a “physical” and identifiable border, unlike the label. This characteristic is controlled by the BorderStyle property which is an enumerator called TStaticBorderStyle and defined as follows:

enum TStaticBorderStyle { sbsNone, sbsSingle, sbsSunken };

Its effects are as follows:

sbsNone sbsSingle sbsSunken

Probably the biggest difference for us is that the StaticText, which is a descendent of TWinControl’s has a Handle property. This allows you to access the properties of the Win32’s STATIC class and apply them to the VCL’s StaticText control. For example, you can draw an etched border around the static text control as follows:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	LONG GWL = GetWindowLong(StaticText1->Handle, GWL_STYLE);
	GWL |= SS_ETCHEDFRAME;
	SetWindowLong(StaticText1->Handle, GWL_STYLE, GWL);
}
//---------------------------------------------------------------------------

In the same way, you can use the rectangular area of the StaticText control to draw, display an icon or a bitmap, or perform any other complicated task allowed by the Win32’s STATIC class.

This example demonstrates that through one or another control, the VCL provides most , if not any, of the functionality you need from Windows controls. For example, instead of writing so many lines of code to get the above look, you could have used either a panel, a group box, a radio group, or a bevel controls, to get the above frame.

 

Home Copyright © 2005-2007 FunctionX, Inc.