This example shows how the WinDrawText function can be used to wrap text within a window by using the DT_WORDBREAK flag. The cchDrawn variable receives the number of characters actually drawn by the WinDrawText function. If this value is zero, no text is drawn and the for loop is exited. This can occur if the vertical height of the window is too short for the entire text. Otherwise, cchDrawn is added to the hTotalDrawn variable to provide an offset into the string for the next call to WinDrawText.

#define INCL_WINWINDOWMGR       /* Window Manager Functions     */
#include <os2.h>

HWND    hwnd;           /* parent window                        */
RECTL  rcl;             /* update region                        */
HPS    hps;             /* presentation-space handle            */
char   *pszText;        /* string                               */
LONG   hText;          /* length of string                     */
LONG  cyCharHeight;     /* set character height                 */
LONG   hTotalDrawn;    /* total characters drawn               */
LONG   hDrawn;         /* characters drawn by WinDrawText      */
LONG   cchText;
LONG   cchTotalDrawn;
LONG   cchDrawn;

hps = WinGetPS(hwnd);          /* get a ps for the entire window */

WinQueryWindowRect(hwnd, &rcl);         /* get window dimensions */

WinFillRect(hps, &rcl, CLR_WHITE);      /* clear entire window   */

cchText =  (LONG)strlen(pszText);       /* get length of string  */
cyCharHeight = 15L;                     /* set character height  */

/* until all chars drawn */
for (cchTotalDrawn = 0;  hTotalDrawn !=  hText;
                        rcl.yTop -= cyCharHeight)
   {
   /* draw the text */

    hDrawn = WinDrawText(hps,     /* presentation-space handle */
        hText -  hTotalDrawn,    /* length of text to draw    */
       pszText +  hTotalDrawn,    /* address of the text       */
       &rcl,                       /* rectangle to draw in      */
       0L,                         /* foreground color          */
       0L,                         /* background color          */
       DT_WORDBREAK | DT_TOP | DT_LEFT | DT_TEXTATTRS);
   if (cchDrawn)
        hTotalDrawn +=  hDrawn;
   else
       break;                      /* text could not be drawn   */
   }

WinReleasePS(hps);                  /* release the ps            */


[Back] [Next]