Including a Static Control in a Client Window

An application can include a static control in a non-dialog window by calling WinCreateWindow with the window class WC_STATIC. The flStyle parameter to WinCreateWindow defines the appearance of the control.

The following code fragment creates a static text control whose size and position are based on the size of the client window and the metrics for the current font:

#define ID_TITLE 5
#define INCL_GPILCIDS

HWND hwnd,hwndStatic,hwndClient;
HPS hps;
RECTL rcl;
FONTMETRICS fm;
ULONG ulTitleLen;
CHAR szTitle[] = "Static Text Controls";

/* Obtain the size of the client window */
WinQueryWindowRect(hwnd, &rcl);

/* Obtain a presentation space handle and */
/* the metrics for the current font       */
hps = WinBeginPaint (hwnd, (HPS) NULL, (PRECTL) NULL);
GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), &fm);

/* Obtain the size of the static control text string */
ulTitleLen = (ULONG) strlen(szTitle);

/* Create the static control. Base the size and  */
/* position on the size of the client window and */
/* the metrics of the current font.              */

hwndStatic = WinCreateWindow(
               hwndClient,                    /* Parent window          */
               WC_STATIC,                     /* Window class           */
               szTitle,                       /* Window text            */
               WS_VISIBLE |                   /* Make it visible        */
               SS_TEXT    |                   /* Static-text control    */
               DT_VCENTER |                   /* Center text vert.      */
               DT_CENTER,                     /* Center text horiz.     */

               ((rcl.xRight / 2) -
               (ulTitleLen / 2) * fm.lEmInc), /* x position             */
               rcl.yTop - fm.lEmHeight * 2,   /* y position             */
               fm.lEmInc * ulTitleLen,        /* Width                  */
               fm.lEmHeight * 2,              /* Height                 */
               hwndClient,                    /* Owner window           */
               HWND_TOP,                      /* Top of z-order         */
               ID_TITLE,                      /* Window identifier      */
               NULL,                          /* Control data           */
               NULL);                         /* Presentation parameters*/

WinEndPaint(hps);

If your application creates a static control with the SS_ICON or SS_BITMAP style, make sure that the resource identifier specified in the pszName parameter corresponds to an icon or a bit map resource in the resource-definition file. If there is no resource, the application cannot create the static control.


[Back: Including a Static Control in a Dialog Window]
[Next: Title-Bar Controls]