Click here to Skip to main content
15,868,010 members
Articles / Desktop Programming / MFC
Article

Using the CStatic control

Rate me:
Please Sign up or sign in to vote.
4.83/5 (28 votes)
23 Mar 2001 525.6K   6.3K   92   97
An entry level tutorial on using the CStatic control

Introduction

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 CStatic control
  • Adding a CStatic to your dialog
  • CStatic Styles
  • Useful Group Box properties
  • Changing Static control Styles at Runtime
  • Changing Contents of a CStatic at Runtime
  • Handling CStatic messages

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)

Image 1

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.

Image 2

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.

CStatic Styles

Picture

  • 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_ICON or SS_BITMAP style) from being resized and if the image is larger than the destination area, the image is clipped.

Image 3

Plain Text

  • 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 WM_CTLCOLOR message.)

Image 4

All

  • SS_NOTIFY Sends the parent window STN_CLICKED, STN_DBLCLK, STN_DISABLE, and 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",
                IDC_STATIC,10,135,175,15,SS_CENTERIMAGE,WS_EX_STATICEDGE

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.

Image 5

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.

void CStaticCtrlTutorialDlg::OnCheckBox1() 
{
	UpdateData(TRUE);
	DWORD dw = m_Var.GetStyle();
	m_strVar = _T("Testing");
	if( dw & SS_CENTER )
		m_Var.ModifyStyle(SS_CENTER,0);
	else
		m_Var.ModifyStyle(0,SS_CENTER|SS_CENTERIMAGE);

	m_Var.Invalidate();
	UpdateData(FALSE);
}

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 UpdateData(FALSE).

.
	m_strVar = _T("Testing");
.
.
	UpdateData(FALSE);
.

Alternatively use

m_Var.SetWindowText( "The New Text" );

To change images in SS_ICON, SS_BITMAP and 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 SetIcon(). 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.

void CStaticCtrlTutorialDlg::OnToggleicon() 
{
	TRACE("CStaticCtrlTutorialDlg::OnToggleicon()\n");
	UpdateData(TRUE);
	if( m_bToggleIcon )
		m_Icon.SetIcon(::LoadIcon(NULL, IDI_QUESTION));
	else
		m_Icon.SetIcon(m_hIcon);

	m_Icon.Invalidate();
	UpdateData(FALSE);
}

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 SS_NOTIFY style

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.

ON_BN_CLICKED(IDC_STATICBITMAP, OnStaticbitmap)
ON_BN_CLICKED(IDC_STATICICON, OnStaticicon)

The following Macros are supported in VC++ 6 for static notifications but must be entered manually.

ON_STN_CLICKED(IDC_STATICBITMAP, OnStaticbitmap)
ON_STN_DBLCLK(IDC_STATICBITMAP, OnBitmapDblClk)
ON_STN_ENABLE(IDC_STATICBITMAP, OnBitmapEnabled)
ON_STN_DISABLE(IDC_STATICBITMAP, OnBitmapDisabled)

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.

Conclusion

The CStatic class is quite simple. You will find fancier versions of Static controls elsewhere on this site.

Happy programming!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer Retired
Australia Australia
Started working with computers when I/O was with punched paper tape, using Algol. Then learned Fortran, Basic, various Assemblers, Forth and Postscript. Built a robot in the early '80s. When I finally got a PC I learned C, C++ and more recently worked on a variety of .NET and PHP projects. Senior Software Engineer at MedTech Global in Melbourne, Australia.

Comments and Discussions

 
QuestionHow to set the color of the border of a static control Pin
huutribk200117-May-06 20:59
huutribk200117-May-06 20:59 
AnswerRe: How to set the color of the border of a static control Pin
Wolfram Steinke18-May-06 9:34
Wolfram Steinke18-May-06 9:34 
QuestionHow to get the X & Y clicked position Pin
Steve Messer31-May-05 19:58
Steve Messer31-May-05 19:58 
AnswerRe: How to get the X & Y clicked position Pin
Wolfram Steinke31-May-05 20:57
Wolfram Steinke31-May-05 20:57 
GeneralRe: How to get the X & Y clicked position Pin
Steve Messer31-May-05 20:59
Steve Messer31-May-05 20:59 
GeneralRe: How to get the X & Y clicked position Pin
Wolfram Steinke31-May-05 21:20
Wolfram Steinke31-May-05 21:20 
GeneralRe: How to get the X & Y clicked position Pin
Steve Messer31-May-05 21:30
Steve Messer31-May-05 21:30 
GeneralRe: How to get the X & Y clicked position Pin
CosmoNova25-Jul-05 21:33
CosmoNova25-Jul-05 21:33 
GeneralRe: How to get the X & Y clicked position Pin
Wolfram Steinke25-Jul-05 22:04
Wolfram Steinke25-Jul-05 22:04 
AnswerRe: How to get the X & Y clicked position Pin
santoshedwin15-Jan-07 21:25
santoshedwin15-Jan-07 21:25 
Questionhow to select text (CStatic) from afxmessagebox or dialog box Pin
Rodrick5-Apr-05 3:58
Rodrick5-Apr-05 3:58 
AnswerRe: how to select text (CStatic) from afxmessagebox or dialog box Pin
Wolfram Steinke5-Apr-05 8:47
Wolfram Steinke5-Apr-05 8:47 
Questionwhy to add SS_CENTERIMAGE? Pin
vividtang15-May-04 13:58
vividtang15-May-04 13:58 
GeneralCStatic runtime Pin
dob_lalema29-Mar-04 2:56
dob_lalema29-Mar-04 2:56 
GeneralRe: CStatic runtime Pin
boomyjee5-Feb-05 4:21
boomyjee5-Feb-05 4:21 
GeneralChange font and size Pin
gildan202012-Mar-04 5:01
gildan202012-Mar-04 5:01 
GeneralRe: Change font and size Pin
Wolfram Steinke12-Mar-04 10:13
Wolfram Steinke12-Mar-04 10:13 
GeneralRe: Change font and size Pin
Member 18590455-Apr-05 21:51
Member 18590455-Apr-05 21:51 
GeneralRe: Change font and size Pin
Anonymous1-Aug-05 11:51
Anonymous1-Aug-05 11:51 
AnswerRe: Change font and size Pin
Member 128624026-Aug-09 3:15
Member 128624026-Aug-09 3:15 
GeneralRe: Change font and size Pin
hema_soft4-Jul-06 2:31
hema_soft4-Jul-06 2:31 
QuestionHow to set the color of the border of a static control? Pin
kevinhu5259-Mar-04 23:07
kevinhu5259-Mar-04 23:07 
AnswerRe: How to set the color of the border of a static control? Pin
Wolfram Steinke12-Mar-04 10:10
Wolfram Steinke12-Mar-04 10:10 
GeneralCStatic text object and bitmap with DoModal. Pin
splach28-Oct-03 9:36
splach28-Oct-03 9:36 
GeneralRe: CStatic text object and bitmap with DoModal. Pin
Wolfram Steinke28-Oct-03 21:06
Wolfram Steinke28-Oct-03 21:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.