<!-- Download Links -->
This tutorial will show how use the
CStatic class in a very basic dialog based application. It will cover the following points:
- The different types of
- Adding a
CStatic to your dialog
- Useful Group Box properties
- Changing Static control Styles at Runtime
- Changing Contents of a
CStatic at Runtime
This tutorial assumes that you are comfortable with creating a dialog based application using the VC++ 6
Class Wizard. If you require information on this topic consult the article A Beginners Guide to Dialog Base Applications - Part 1.
Make sure you enable ActiveX control if you wish to use Enhanced Meta Files.
As the name implies, Static controls are mostly used for displaying information in a dialog, and are basically a Read Only controls, and will accept no input like Edit controls. The CStatic class however has a number of notification messages as you will see below.
The different types of CStatic control
There are a variety of Static control ranging from plain text to ones that display images, as you can see in the image below.
- Plain text Statics which can have 3D borders
- Images that use Icons, Animated Cursors or Bitmaps resources, or Enhanced Meta Files
- Different Style Frames
- Different Rectangles
- Group Boxes (which are actually buttons but looks like statics)
I have included the Group Box in this tutorial because it is often used as static for decorative purposes. It does however have some interesting properties that are worth mentioning, and can be useful.
Adding a CStatic to your dialog
When your dialog-based application is generated, go to the Resources in the Workspace window. Select the dialog
IDD_STATICCTRLTUTORIAL_DIALOG in the Dialog section. To insert the Static control, select it from the Control palette. The cross shaped cursor indicates where the
centre of the Static control will be placed.
Static Text is used for placing areas of text on the dialog.
Picture covers Frames, Icons, Bitmaps, Rectangles and Enhanced Meta Files
Drag the edges of the control out the desired size.
By default these controls are given a resource ID of -1 (
IDC_STATIC). If you require any notifications from the control or want to change text at runtime etc. this ID must be changed keeping in mind the usual restrictions for resource Ids.
SS_BLACKFRAME Draws a box with a frame drawn with the same color as window frames. The default is black.
SS_GRAYFRAME Draws a box with a frame drawn with the same color as the screen background (desktop). The default is gray.
SS_WHITEFRAME Draws a box with a frame drawn with the same color as the window background. The default is white.
SS_ETCHEDFRAME Draws the frame of the static control using the
EDGE_ETCHED edge style.
SS_BLACKRECT Draws a rectangle filled with the color used to draw window frames. The default is black.
SS_GRAYRECT Draws a rectangle filled with the color used to fill the screen background. The default is gray.
SS_WHITERECT Draws a rectangle filled with the color used to fill the window background. The default is white.
SS_BITMAP Draws a bitmap in the static control. The text is the name of a bitmap (not a filename) defined elsewhere in the resource file. The style ignores the nWidth and nHeight parameters; the control automatically sizes itself to accommodate the bitmap.
SS_ICON Draws an icon displayed in the dialog box. The given text is the name of an icon (not a filename) defined elsewhere in the resource file. The nWidth and nHeight parameters are ignored; the icon automatically sizes itself. The Icon can be an animated cursor.
SS_CENTERIMAGE Specifies that the midpoint of a static control with the
SS_BITMAP style will remain fixed when you resize the control. The four sides are adjusted to accommodate a new bitmap. If the bitmap is smaller than the control's client area, the rest of the client area is filled with the color of the pixel in the upper-left corner of the bitmap. A plain text (
SS_SIMPLE) control will have its text vertically centered.
SS_REALSIZEIMAGE Prevents a static icon or bitmap control (that is, static controls that have the
SS_BITMAP style) from being resized and if the image is larger than the destination area, the image is clipped.
SS_LEFT Designates a simple rectangle and displays the given text flush-left in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next flush-left line.
SS_CENTER Designates a simple rectangle and displays the given text centered in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next centered line.
SS_RIGHT Designates a simple rectangle and displays the given text flush-right in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next flush-right line.
SS_LEFTNOWORDWRAP Designates a simple rectangle and displays the given text flush-left in the rectangle. Tabs are expanded, but words are not wrapped. Text that extends past the end of a line is clipped.
SS_NOPREFIX This style specifies, Windows will not interpret any ampersand (&) characters in the control’s text to be accelerator prefix characters. That is, the ampersand (&) is not removed and the next character in the string is underlined.
SS_SIMPLE Designates a simple rectangle and displays a single line of text flush-left in the rectangle. The line of text cannot be shortened or altered in any way. (The control’s parent window or dialog box must not process the
SS_NOTIFY Sends the parent window
STN_ENABLE notification messages when the user clicks or double-clicks the control.
- There are also a few border style which in the main are self explanatory, and under Windows 2000 a few new ones have been introduced for text
formatting with Ellipsis.
If you have a look at the resource script you will see the following entry for one of the controls. Note that the
SS_CENTER style is implied by the
CTEXT control type.
CTEXT "Plain Text Label - SS_CENTER | SS_CENTERIMAGE",
The alternative entry which results in the same visual effect is as follow. Here the styles are fully specified and the control type is specified as Static.
CONTROL "Plain Text Label - SS_CENTER | SS_CENTERIMAGE",IDC_STATIC,
"Static",SS_CENTER | SS_CENTERIMAGE,10,135,175,15,WS_EX_STATICEDGE
Other Etched Style
SS_ETCHEDHORZ Draws the top and bottom edges of the static control using the
EDGE_ETCHED edge style.
SS_ETCHEDVERT Draws the left and right edges of the static control using the
EDGE_ETCHED edge style.
These 2 styles can not be set using the VC++ 6 resource editor, and if you edit the RC file in text mode these styles will be replaced by
SS_ETCHEDFRAME the next time you use the resource editor. You can use them however in the Create() function.
If you wish to have an etched looking vertical or horizontal line use the
SS_ETCHEDFRAME style an make the rectangle 1 unit high or wide.
Useful Group Box properties
As stated above a Group Box is in actual fact a button control, so it is out of place in this
tutorial. However is does give the appearance of a static control and is mostly used in the same way.
You will notice in the demo program that "Group 1" has an accelerator key associated with it as indicated by the underlined 1. The Group Box also has the
WS_TAPSTOP style. This results in the feature, that when the accelerator key 'ALT+1' is press, focus is given to the first control in that group or as in this case the selected radio button.
The first (top) control in the group must have the
WS_GROUP style and the first control after the end of the group must also have the
WS_GROUP style, to indicate the first control in the next group.
Changing Edit control Styles at Runtime
It is possible to change some styles at runtime using
ModifyStyle( DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0 ), although this is not done frequently.
The code segment below toggles the
SS_CENTER style of the control, making the jump between the left and the center of the control. It checks the current style of the control and then adds or removes the
SS_CENTER style accordingly.
DWORD dw = m_Var.GetStyle();
m_strVar = _T("Testing");
if( dw & SS_CENTER )
Note for this trick to work you must force a redraw of the control by calling
Invalidate(). Also if
UpdateData() is not called at the start of the function
GetStyle() will not return the correct value.
Changing Contents of a CStatic at Runtime
The code segment above also demonstrates the simplest way to change the text in the control. By using Data Exchange simply attach a string variable to the control, assign a string to it and call
m_strVar = _T("Testing");
m_Var.SetWindowText( "The New Text" );
To change images in
SS_ENHMETAFILE styles have functions that can be called.
The code below toggles the icon between the application's icon and the system 'query' icon, using
SetCursor() can also be used. Bitmaps and icons can be assigned in the resource editor, and this is the usual method when they are not changed.
if( m_bToggleIcon )
For bitmaps use
SetBitmap() and use
SetEnhMetaFile() for enhanced meta files.
if (m_EMF.GetEnhMetaFile() == NULL)
m_EMF.SetEnhMetaFile( ::GetEnhMetaFile(_T("ms.emf")) );
Note for enhanced meta files to work like this
AfxEnableControlContainer() must be called in the Application
InitInstance() function, to enable ActiveX (OLE) controls.
Handling CStatic messages
The notification messages available for Static controls are the following. The Static control must have
STN_CLICKED notification message when the user clicks on the Static control.
STN_DBLCLK notification message when the user double-clicks on the Static control.
STN_ENABLED notification message when a Edit control loses the keyboard focus.
STN_DISABLED notification message when the user cancels the selection in a Edit control.
Class Wizard will only set up a function to handle the
STN_CLICKED notification. If you look at the demo you will note that Class Wizard inserts the following lines to handle the clicked message on the Static controls.
The following Macros are supported in VC++ 6 for static notifications but must be entered manually.
The handlers in the demo do nothing more than report they have been called, by executing a
TRACE statement and changing the text in another Static control.
The CStatic class is quite simple. You will find fancier versions of Static controls elsewhere on this site.