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. |
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:
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 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; } //---------------------------------------------------------------------------
|
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.
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:
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 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:
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:
To provide other frame styles, use the options of the BevelInner, the BevelKind, and the BevelOuter properties.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|