Home

Windows Controls: The Static Text

 

Introduction to the Static Text Control

 

Description

The label control is a native VCL object designed to show text. In fact, it is a graphical control and not a genuine Windows text-based control. To help you create a true text-based label, the Win32 library provides its the static text control.

In Microsoft Windows, a static control is many objects under one name. One version of a static control is used to display pictures (the VCL provides the image and the paint box for that purpose. Another version of the static control can be used as the starting point of various types of controls (this qualifies as an owner-drawn static control). Another version can be used to display text. One more version can be used for its aesthetic abilities. Here, we are interested in the latter two versions: static text and aesthetic object.

 

Practical LearningPractical Learning: Introducing the Static Text

  1. To create a new application, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  2. In the Object Inspector, change the proeprties of the form as follows:
    Caption: Four-Corner Supermarket
    Name: frmMoneyChange
 

Creating a Static Text Control

In Microsoft Windows, a static control is an object created using the STATIC class. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnStaticControlClick(TObject *Sender)
{
	CreateWindowEx(0,
		       "STATIC",
		       NULL,
		       WS_CHILD | WS_VISIBLE,
		       10, 10, 124, 25,
		       Handle,
		       NULL,
		       HInstance,
		       NULL);
}
//---------------------------------------------------------------------------

To upport the Win32 STATIC control, the VCL provides the TStaticText class. The TStaticText class is based on the TCustomStaticText class that actually provides all of its functionality. The TCustomStaticText class is derived from TWinControl:

TStaticText Inheritance

Notice that the TStaticText class is derived from TWinControl while the the TLabel class comes from TGraphicControl.

To visually add a static text control to your application, from the Additional section of the Tool Palette, click the TStaticText button TStaticText and click the container that would host it. To maually get a static text control, create a pointer to TStaticText and initialize it appropriately. In its constructor, specify its owner as the object that will host it. Because this is a Windows control, make sure you specify its parent. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnStaticControlClick(TObject *Sender)
{
	TStaticText *txtControl = new TStaticText(this);
	txtControl->Parent = this;
}
//---------------------------------------------------------------------------

Practical LearningPractical Learning: Creating a Static Text

  1. In the Tool Palette, click Additional
  2. In the Additional section, click the TStaticText icon TStaticText
  3. Click the form
  4. In the Object Inspector, click Caption
  5. Type Amount Owed to Customer

Characteristics of a Static Text Control

 

Introduction

As mentioned already, the static text is a Windows control. As such, it has a Windows handle that allows it to customize a STATIC control. To get that handle, access the Handle property that the class inherits from the TWinControl class. Besides the handle, as a visual control, a static text also has a location as its Left and Top values, and a size as its Width and Height properties. Actually, when it comes to its size, the actual length of the static text control influences this detail.

It also has the following properties similar to those of the label: FocusControl, and ShowAccelChar.

The Size of the Control and its Caption

As stated already, in Microsoft Windows, the static text control can be used either or both to display text and/or for its aesthetic features. If you want the control to display text, at design time, you can specify it in the Caption field of the Object Inspector. Of course, to programmatically specify the text, access the Caption property and assign the desired string to it. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnStaticControlClick(TObject *Sender)
{
	TStaticText *txtControl = new TStaticText(this);
	txtControl->Parent = this;

	txtControl->Caption = "Enhancements";
}
//---------------------------------------------------------------------------

After specifying the caption of the control, the operating system allocates enough room for the control. This is based on the fact that the static text control uses the AutoSize Boolean property. If this property is set to True, the operation system will always find out the length of the caption and provide just enough room for it. If you use the control for its aesthetic, you may want to control its size. In this case, set its AutoSize property to False.

If you set the AutoSize property to False, you can use a room larger than the caption actually needs. Consider the following:

Static Text

Within the area allocated to the control, you can align the caption to the left, the center, or the right side. This is possible because the TStaticText control is equipped with the Alignment property.

The Border Style

 The static text 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
Static Text: Caption Alignment Static Text: Caption Alignment Static Text: Caption Alignment

The Frame

To enhance the features of a static text control, the Win32 added a frame to it. The frame is controlled by some styles that must be applied to the control. To apply a style, fitst get its handle and pass it to the GetWindowLongPtr() function. Using the OR bit operator "|", assign the desired style to the control. To apply the style, call the SetWindowLongPtr() function. The available styles are:

  • SS_BLACKFRAME:
    //---------------------------------------------------------------------------
    void __fastcall TForm4::FormCreate(TObject *Sender)
    {
    	LONG GWL = GetWindowLongPtr(StaticText1->Handle, GWL_STYLE);
    	GWL |= SS_BLACKFRAME;
    	SetWindowLongPtr(StaticText1->Handle, GWL_STYLE, GWL);
    }
    //---------------------------------------------------------------------------
    Static Text: Frame
  • SS_ETCHEDFRAME:
    //---------------------------------------------------------------------------
    void __fastcall TForm4::FormCreate(TObject *Sender)
    {
    	LONG GWL = GetWindowLongPtr(StaticText1->Handle, GWL_STYLE);
    	GWL |= SS_ETCHEDFRAME;
    	SetWindowLongPtr(StaticText1->Handle, GWL_STYLE, GWL);
    }
    //---------------------------------------------------------------------------
    Static Text: Frame

To provide other frame styles, use the options of the BevelInner, the BevelKind, and the BevelOuter properties.

Practical LearningPractical Learning: Specifying the Text of an Edit Control

  1. Complete the design of the form as follows:
     
    Four-Corner Supermarket
    =+= All controls are the TStaticText TStaticText
    =+= All fonts are: Font: Tiffany Lt BT (*)
                   
    Font style: Demi
    =+= The static text with 000 has a Font Size of 36. All the other static text controls have a Font Size of 28
    =+= The static text with 000 has an Alignment of taRightJustify
    =+= The bottom static text has an Align value of alBottom
    AutoSize BevelInner BevelKind BevelOuter Caption Color Font Color Name
            Amount Owed to Customer:   Black  
            000    Maroon stAmountOwed
    False bvLowered bkTile bvRaised 000 clNavy    
      bvRaised bkTile bvRaised  Currency Bills clMaroon  Yellow  
      bvRaised bkTile bvRaised   Coins  clMaroon Yellow  
            Fifties:    Black  
            00   Blue stFifties
            Quarters:   Black  
            00   Blue stQuarters
            Twenties:   Black  
            00   Blue  
            Dimes:   Black  
            00   Blue stDimes
            Tens:   Black  
            00   Blue stTens
            Nickels:   Black  
            00   Blue stNickels
            Fives:   Black  
            00   Blue stFives
            Pennies:   Black  
            00   Blue stPennies
            Ones:   Black  
            00   Blue stOnes
    False bvRaised bkSoft bvRaised        

    (*) If you don't have the "Tiffany Lt BT" font, select any Serif font of your choice (such as Garamond, Georgia, or Times New Roman)

  2. Double-click an unoccupied area of the form (such as the area under Pennies) to generate its OnCreate event
  3. Implement the event as follows:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "MoneyChange.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMoneyChange *frmMoneyChange;
    //---------------------------------------------------------------------------
    __fastcall TfrmMoneyChange::TfrmMoneyChange(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMoneyChange::FormCreate(TObject *Sender)
    {
    	// This variable is the value that will be considered
    	UnicodeString strAmount;
    	// This is the actual monetary value that will be used
    	double Amount    = 0.00;
    	// This will be an accessory used during calculation
    	double NewAmount = 0.00;
    	int    Decimals  = 0;
    	// These are the values that will be displayed
    	int    Fifties   = 0;
    	int    Twenties  = 0;
    	int    Tens      = 0;
    	int    Fives     = 0;
    	int    Ones      = 0;
    	int    Quarters  = 0;
    	int    Dimes     = 0;
    	int    Nickels   = 0;
    	int    Pennies   = 0;
    
    	strAmount =  InputBox(L"Four-Corner Supermarket",
    						  L"Enter the amount owed to the customer:",
    						  L"0.00");
    
    	int IndexOfPeriod = strAmount.Pos(L".");
    
    	// This variable is used to locate the decimal side
    	UnicodeString strDecimals = strAmount.SubString(IndexOfPeriod+1, 2);
    	Amount = StrToFloat(strAmount);
    	Decimals = StrToInt(strDecimals);
    
    	if (Amount > 50)
    	{
    		Fifties = (int)(Amount / 50);
    		NewAmount = Amount - 50;
    		Twenties = (int)(NewAmount / 20);
    		NewAmount -= (Twenties * 20);
    		Tens = (int)(NewAmount / 10);
    		NewAmount -= (Tens * 10);
    		Fives = (int)(NewAmount / 5);
    		NewAmount -= (Fives * 5);
    		Ones = (int)(NewAmount);
    	}
    	else
    	{
    		NewAmount = Amount;
    		Twenties = (int)(NewAmount / 20);
    		NewAmount -= (Twenties * 20);
    		Tens = (int)(NewAmount / 10);
    		NewAmount -= (Tens * 10);
    		Fives = (int)(NewAmount / 5);
    		NewAmount -= (Fives * 5);
    		Ones = (int)(NewAmount);
    	}
    
    	Quarters = (int)(Decimals / 25);
    	Decimals -= Quarters * 25;
    	Dimes = (int)(Decimals / 10);
    	Decimals -= Dimes * 10;
    	Nickels = (int)(Decimals / 5);
    	Decimals -= Nickels * 5;
    	Pennies = Decimals;
    
    	stAmountOwed->Caption = strAmount;
    
    	stFifties->Caption = IntToStr(Fifties);
    	stTwenties->Caption = IntToStr(Twenties);
    	stTens->Caption = IntToStr(Tens);
    	stFives->Caption = IntToStr(Fives);
    	stOnes->Caption = IntToStr(Ones);
    	stQuarters->Caption = IntToStr(Quarters);
    	stDimes->Caption = IntToStr(Dimes);
    	stNickels->Caption = IntToStr(Nickels);
    	stPennies->Caption = IntToStr(Pennies);
    }
    //---------------------------------------------------------------------------
  4. Press F9 to execute application
  5. When the input box displays, type 12.34 and press Enter
     
    Four-Corner Supermarket
  6. Close the form and return to your programming environment
  7. Press F9 again to execute the application
  8. When the input box displays, type 6.05 and press Enter
     
    Four-Corner Supermarket
  9. Close the form and return to your programming environment
  10. Press F9 again to execute the application
  11. When the input box displays, type 88.68 and press Enter
     
    Four-Corner Supermarket
  12. Close the form and return to your programming environment
 
 
 

Home Copyright © 2010-2016, FunctionX