Click here to Skip to main content
Click here to Skip to main content

Using the CStatic control

, 23 Mar 2001
Rate this:
Please Sign up or sign in to vote.
An entry level tutorial on using the CStatic control
<!-- Download Links -->

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)

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.

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.

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.)

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.

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

Share

About the Author

Wolfram Steinke
Engineer MedTech Global
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 color backgrond of CStatic control PinmemberSachin Shinde28-Jul-14 2:17 
GeneralMy vote of 5 Pinmemberdu[DE]20-Mar-13 5:16 
GeneralMy vote of 1 PinmemberMember 77496843-Jul-11 4:45 
Generaljoy.william102@yahoo.co.uk Pinmemberjoy202028-Oct-10 0:11 
QuestionHow to handle WM_CREATE or WM_SHOWWINDOW? Pinmemberwufeng052411-Jun-07 23:26 
AnswerRe: How to handle WM_CREATE or WM_SHOWWINDOW? PinmemberWolfram Steinke12-Jun-07 0:50 
QuestionSTATIC-Control with bitmap doesn't resize itself Pinmemberentwicklung@zamik.de13-Apr-07 4:49 
Questionwhy the image doesn't appear ???? Pinmembersingersinger11-Feb-07 1:19 
QuestionHow to chage a icon assigned to CStatic through global function??? Pinmembergloriousgopi29-Oct-06 23:23 

Hi
 
Can any one provide me some help as how to chage the icon assigned to a CStatc control from a global function.
 
I have created a CStatic control and assigned a Icon image to it by changing its type to Icon.
 
Now i want to change the another icon from a global fuction. i already gathered the icon by using the LoadIcon() fuction.
 
Thanks in advance,
 

 
Gopinath MV

GeneralHandling single click and double click both Pinmembermandabi17-Jul-06 0:35 
QuestionHow to set the color of the border of a static control Pinmemberhuutribk200117-May-06 20:59 
AnswerRe: How to set the color of the border of a static control PinmemberWolfram Steinke18-May-06 9:34 
QuestionHow to get the X &amp; Y clicked position Pinmembersmesser31-May-05 19:58 
AnswerRe: How to get the X &amp; Y clicked position PinmemberWolfram Steinke31-May-05 20:57 
GeneralRe: How to get the X &amp; Y clicked position Pinmembersmesser31-May-05 20:59 
GeneralRe: How to get the X &amp; Y clicked position PinmemberWolfram Steinke31-May-05 21:20 
GeneralRe: How to get the X &amp; Y clicked position Pinmembersmesser31-May-05 21:30 
GeneralRe: How to get the X &amp; Y clicked position PinmemberCosmoNova25-Jul-05 21:33 
GeneralRe: How to get the X &amp; Y clicked position PinmemberWolfram Steinke25-Jul-05 22:04 
AnswerRe: How to get the X &amp; Y clicked position Pinmembersantoshedwin15-Jan-07 21:25 
Questionhow to select text (CStatic) from afxmessagebox or dialog box PinmemberRodrick5-Apr-05 3:58 
AnswerRe: how to select text (CStatic) from afxmessagebox or dialog box PinmemberWolfram Steinke5-Apr-05 8:47 
Questionwhy to add SS_CENTERIMAGE? Pinmembervividtang15-May-04 13:58 
GeneralCStatic runtime Pinmemberdob_lalema29-Mar-04 2:56 
GeneralRe: CStatic runtime PinsussBoomyJee5-Feb-05 4:21 
GeneralChange font and size Pinmembergildan202012-Mar-04 5:01 
GeneralRe: Change font and size PinmemberWolfram Steinke12-Mar-04 10:13 
GeneralRe: Change font and size PinsussRohit Singh5-Apr-05 21:51 
GeneralRe: Change font and size PinsussAnonymous1-Aug-05 11:51 
AnswerRe: Change font and size PinmemberMember 128624026-Aug-09 3:15 
GeneralRe: Change font and size Pinmemberhema_soft4-Jul-06 2:31 
QuestionHow to set the color of the border of a static control? Pinmemberkevinhu5259-Mar-04 23:07 
AnswerRe: How to set the color of the border of a static control? PinmemberWolfram Steinke12-Mar-04 10:10 
GeneralCStatic text object and bitmap with DoModal. Pinmembersplach28-Oct-03 9:36 
GeneralRe: CStatic text object and bitmap with DoModal. PinmemberWolfram Steinke28-Oct-03 21:06 
GeneralSlow Animated Cursor Pinmembermot15-Oct-03 20:27 
GeneralImage in Static Control PinmemberJohn Black19-Aug-03 19:56 
GeneralRe: Image in Static Control PinmemberJohann Gerell19-Aug-03 21:13 
Questionwhy &quot;SS_SUNKEN&quot; don't work? Pinmembersend_ch17-Aug-03 2:14 
GeneralBitmap Change Assertion Error PinsussAnonymous2-May-03 5:55 
GeneralBitmap attached to button PinsussAnonymous28-Mar-03 18:07 
GeneralRe: Bitmap attached to button PinmemberWolfram Steinke3-Apr-03 18:41 
GeneralUse bitmap as button Pinmembertsosnoski26-Mar-03 16:08 
GeneralRe: Use bitmap as button PinmemberWolfram Steinke26-Mar-03 22:44 
GeneralAutomatic Sizing Static PinmemberGodBeastX17-Mar-03 6:45 
GeneralRe: Automatic Sizing Static PinmemberWolfram Steinke26-Mar-03 22:49 
GeneralCStatic Control backcolor PinsussAnonymous16-Feb-03 0:29 
GeneralRe: CStatic Control backcolor PinmemberWolfram Steinke16-Feb-03 7:04 
Generalmodal frame Pinmembershotgun20-Oct-02 8:45 
Questionhow to use settooltip to show a text in a CStatic Pinmemberageng22-Sep-02 22:20 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 24 Mar 2001
Article Copyright 2000 by Wolfram Steinke
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid