Although static controls can be used in overlapped, pop-up, and child windows, they are designed for use in dialog boxes, where the system standardizes their behavior. By using static controls outside dialog boxes, a developer increases the risk that the application might behave in a nonstandard fashion. Typically, a developer either uses static controls in dialog boxes or uses the SS_OWNERDRAW style to create customized static controls.
For more information about dialog boxes, see Dialog Boxes. For more information about window classes, see Window Classes. For more information about window subclassing, see Window Procedures.
The following topics are discussed in this section.
There are four types of static controls. Each type has one or more styles.
For a table of static control styles, see Static Control Styles.
A simple graphics static control displays a frame or a filled rectangle. A frame can be drawn in a number of styles, included black, gray, or white. In addition, a frame can be drawn with an etched style to give it a three-dimensional appearance. The frame styles include SS_BLACKFRAME, SS_GRAYFRAME, SS_WHITEFRAME, SS_ETCHEDHORZ, SS_ETCHEDVERT, and SS_ETCHEDFRAME.
A rectangle can be filled with color in one of three styles: black, gray, or white. These styles are defined by the constants SS_BLACKRECT, SS_GRAYRECT, and SS_WHITERECT.
A text static control displays text in a rectangle in one of five styles:
These styles are defined by the constants SS_LEFTNOWORDWRAP, SS_LEFT, SS_CENTER, SS_RIGHT, and SS_SIMPLE, respectively. The system rearranges the text in these controls in predefined ways, except for "simple" text, which is not rearranged.
An application can change the text in a text static control at any time by using the SetWindowText function or the WM_SETTEXT message.
The system displays as much text as it can in the static control and clips whatever does not fit. To calculate an appropriate size for the control, retrieve the font metrics for the text. For more information about fonts and font metrics, see Fonts and Text.
An image static control can display bitmaps, icons (including animated icons), or enhanced metafiles. The type of graphic that a particular static control displays depends on the control's style: SS_BITMAP, SS_ICON, or SS_ENHMETAFILE. An application specifies the style when it creates the control and also specifies a handle to the bitmap, icon, or metafile for the control to display. After the control is created, an application can associate a different graphic with the control by sending it an STM_SETIMAGE message, specifying a handle to the new graphic object. An application can retrieve a handle to the graphic object currently associated with a static control by sending it an STM_GETIMAGE message. An application sends messages to a static control by using the SendDlgItemMessage function.
By using the SS_OWNERDRAW style, an application can take responsibility for painting a static control. The parent window of an owner-drawn static control (its owner) receives a WM_DRAWITEM message whenever the static control needs to be painted. The message includes a pointer to a DRAWITEMSTRUCT structure that contains information that the owner window uses when drawing the control.
The window procedure for the predefined static control window class performs default processing for all messages that the static control procedure does not process. When the static control returns FALSE for any message, the predefined window procedure checks the messages and carries out the default action described in the following table. In the table, a text static control is a static control with the style SS_LEFTNOWORDWRAP, SS_LEFT, SS_CENTER, SS_RIGHT, or SS_SIMPLE.
Message | Default action |
---|---|
WM_CREATE | Loads the graphic object and sizes the window to the object's size, for graphic static controls. Takes no action for other static controls. |
WM_DESTROY | Frees and destroys any graphic object, for graphic static controls. Takes no action for other static controls. |
WM_ENABLE | Repaints visible static controls. |
WM_ERASEBKGND | Returns TRUE, indicating the control erases the background. |
WM_GETDLGCODE | Returns DLGC_STATIC. |
WM_GETFONT | Returns a handle to the font for text static controls. |
WM_GETTEXT | Returns the number of characters copied. |
WM_GETTEXTLENGTH | Returns the length, in characters, of the text for a text static control. |
WM_LBUTTONDBLCLK | Sends the parent window an STN_DBLCLK notification message if the control style is SS_NOTIFY. |
WM_LBUTTONDOWN | Sends the parent window an STN_CLICKED notification message if the control style is SS_NOTIFY. |
WM_NCLBUTTONDBLCLK | Sends the parent window an STN_DBLCLK notification message if the control style is SS_NOTIFY. |
WM_NCLBUTTONDOWN | Sends the parent window an STN_CLICKED notification message if the control style is SS_NOTIFY. |
WM_NCHITTEST | Returns HTCLIENT if the control style is SS_NOTIFY; otherwise, returns HTTRANSPARENT. |
WM_PAINT | Repaints the control. |
WM_SETFONT | Sets the font and repaints for text static controls. |
WM_SETTEXT | Sets the text and repaints for text static controls. |
The predefined window procedure passes all other messages to DefWindowProc for default processing.