home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv3_5 / dialog / dlgtemp.c < prev   
Encoding:
C/C++ Source or Header  |  1989-03-18  |  13.3 KB  |  356 lines

  1. /* DLGTEMP.C -- version 1.00
  2.  *              This module supplies the necessary functions
  3.  *              used to create a dialog box template in global
  4.  *              memory and to pass the handle on to either the
  5.  *              DialogBoxIndirect or CreateDialogIndirect functions.
  6.  */
  7.  
  8. #include <windows.h>
  9. #include "dlgbox.h"
  10.  
  11. /*  forward references */
  12. WORD   lstrlen( LPSTR );
  13. void   SetStyleClass(int);
  14.  
  15. HANDLE   hDlgTemplate; /* Handle to current dialog template memory   */
  16. WORD     wOffset;      /* Current memory offset (updated by CDH & CDI) */
  17. BYTE     iItems;       /* number of items in dialog */
  18. DLGITEM  DlgItem;      /* Dialog item structure */
  19.  
  20. /* ------------- Create Dialog Header ---------------------------*/
  21.  
  22. /*  
  23.  *  This routine allocates a piece of global memory
  24.  *  and then fills in the dialog header structure and saves the
  25.  *  information in global memory.
  26.  */
  27.  
  28. BOOL FAR PASCAL CreateDialogHeader( Style, ItemCount, X, Y, cX, cY, Resource, Class, Caption)
  29. LONG  Style;     /* Dialog box Style */
  30. BYTE  ItemCount; /* Control count for dialog box */
  31. int   X;         /* Dialog box top left column */
  32. int   Y;         /* Dialog box top row */ 
  33. int   cX;        /* Dialog box width */
  34. int   cY;        /* Dialog box height */
  35. LPSTR Resource;  /* Dialog box resource string */
  36. LPSTR Class;     /* Dialog box class string */
  37. LPSTR Caption;   /* Dialog box caption */
  38. {
  39.  
  40.    WORD   ResourceLength, /* Length of resource string */
  41.           ClassLength,    /* Length of class string */
  42.           CaptionLength;  /* Length of caption String */
  43.    DWORD  dwMemLength;    /* Dialog header memory allocation length */
  44.    LPSTR  lpHdrData;      /* Long pointer to locked dialog template memory */
  45.    DLGHDR DlgHdr;         /* Dialog header structure */
  46.    LPSTR  lpDlgHdr;       /* Long pointer to dialog header structure */
  47.    int    i;              /* Loop index */
  48.  
  49.    /* Initialize memory offset */
  50.  
  51.    wOffset = 0;  /* set memory offset to ZERO */
  52.    iItems  = 0;  /* set number of items in dialog to ZERO */
  53.  
  54.    /* Determine string lengths (including terminating null ) */
  55.  
  56.    ResourceLength = lstrlen( Resource ) + 1;
  57.    ClassLength    = lstrlen( Class ) + 1;
  58.    CaptionLength  = lstrlen( Caption ) + 1;
  59.  
  60.    /* Determine length of memory to allocate for dialog header */
  61.    dwMemLength = (DWORD)( sizeof( DLGHDR ) + ResourceLength + ClassLength + CaptionLength );
  62.  
  63.    /* Allocate dialog template memory for dialog header and obtain handle */
  64.  
  65.    if ( !( hDlgTemplate = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, dwMemLength ) ) ) {
  66.       MessageBox( GetFocus(), (LPSTR)"GlobalAlloc Error", (LPSTR)"CDH", MB_OK );
  67.  
  68.       return FALSE; /* GlobalAlloc failed */
  69.    }
  70.  
  71.    /* Lock allocated memory for modification */
  72.    if ( lpHdrData = GlobalLock( hDlgTemplate ) ) {
  73.  
  74.       /* Set dialog header structure data to passed in parameters */
  75.       DlgHdr.dtStyle     = Style;
  76.       DlgHdr.dtItemCount = 1;      /* set to zero */
  77.       DlgHdr.dtX         = X;
  78.       DlgHdr.dtY         = Y;
  79.       DlgHdr.dtCX        = cX;
  80.       DlgHdr.dtCY        = cY;
  81.  
  82.       /* Get pointer to dialog header structure */
  83.       lpDlgHdr = (LPSTR)&DlgHdr;
  84.  
  85.       /* Copy dialog header structure to allocated memory */
  86.       for ( i = 0; i < sizeof( DLGHDR ); i++ )
  87.          *lpHdrData++ = *lpDlgHdr++;
  88.  
  89.       /* Copy resource string to allocated memory */
  90.       while ( ( *lpHdrData++ = *Resource++ ) );
  91.  
  92.       /* Copy class string to allocated memory */
  93.       while ( ( *lpHdrData++ = *Class++ ) );
  94.  
  95.       /* Copy caption string to allocated memory */
  96.       while ( ( *lpHdrData++ = *Caption++ ) );
  97.  
  98.       /* Adjust memory offset past memory allocated for dialog header */
  99.       wOffset += (WORD)( sizeof( DLGHDR ) + ResourceLength + ClassLength + CaptionLength );
  100.  
  101.       /* Unlock allocated memory */
  102.       GlobalUnlock( hDlgTemplate );
  103.  
  104.       return TRUE;  /* everything worked so far, return TRUE */
  105.    }
  106.    else {
  107.       MessageBox( GetFocus(), (LPSTR)"GlobalLock Error", (LPSTR)"CreateDialogHeader", MB_OK );
  108.       GlobalFree( hDlgTemplate ); /* free allocated memory */
  109.       return FALSE;    /* return null handle indicating failure */
  110.    }
  111. }
  112.  
  113. /* ------------- Create Dialog Item ---------------------------*/
  114. /*  
  115.  *  This routine fills in the dialog item structure and 
  116.  *  saves the information in global memory after resizing it.
  117.  */
  118.  
  119. BOOL FAR PASCAL CreateDialogItem( iCtrlID, lStyle, Class, X, Y, cX, cY, Text, ExtraBytes )
  120. int    iCtrlID;      /* Control ID                */
  121. LONG   lStyle;       /* Control style             */
  122. BYTE   Class;        /* Control class             */
  123. int    X;            /* Control top left column   */
  124. int    Y;            /* Control top row           */
  125. int    cX;           /* Control width             */
  126. int    cY;           /* Control height            */
  127. LPSTR  Text;         /* Control text              */
  128. BYTE   ExtraBytes;   /* Control extra bytes count */
  129. {
  130.   LPSTR    lpCtrlData;  /* Long pointer to locked dialog template memory  */
  131.   LPSTR    lpDlgItem;   /* Long pointer to dialog control structure       */
  132.   int      i;           /* Loop index                                     */
  133.   WORD     TextLength;  /* Length of control text string                  */
  134.   DWORD    dwMemLength;
  135.   HANDLE   hCurDlgTemp;
  136.  
  137.   DlgItem.dtilX  = X;
  138.   DlgItem.dtilY  = Y;
  139.   DlgItem.dtilCX = cX;
  140.   DlgItem.dtilCY = cY;
  141.   DlgItem.dtilID = iCtrlID;
  142.  
  143.   if (Class == (BYTE)0)
  144.     SetStyleClass( (int)lStyle);
  145.   else {
  146.       DlgItem.dtilControlClass = Class; /* default, may change in next function */
  147.       DlgItem.dtilStyle = lStyle;
  148.   }
  149.   TextLength  = lstrlen( Text ) + 1;
  150.   dwMemLength = (DWORD)(wOffset + sizeof(DLGITEM) + TextLength + sizeof(BYTE) );
  151.   hCurDlgTemp = GlobalReAlloc( hDlgTemplate, dwMemLength, GMEM_MOVEABLE);
  152.  
  153.   if ( hCurDlgTemp == NULL) { 
  154.      MessageBox ( GetFocus(), (LPSTR)"global lock", (LPSTR)"Failed!", MB_OK);
  155.      GlobalFree( hCurDlgTemp );
  156.      return FALSE;
  157.   }
  158.   hDlgTemplate = hCurDlgTemp;
  159.  
  160.    /* Adjust pointer to reallocated memory bypassing existing data */
  161.  
  162.    if ( (lpCtrlData = GlobalLock(hDlgTemplate)) == NULL) {
  163.      MessageBox ( GetFocus(), (LPSTR)"global lock", (LPSTR)"Failed!", MB_OK);
  164.      GlobalFree( hDlgTemplate );
  165.    }
  166.  
  167.    lpCtrlData += wOffset;
  168.    
  169.    /* Get pointer to dialog control structure */
  170.       lpDlgItem = (LPSTR)&DlgItem;
  171.  
  172.    /* Copy dialog control structure to allocated memory */
  173.       for ( i=0; i<sizeof( DLGITEM ); i++ )
  174.          *lpCtrlData++ = *lpDlgItem++;
  175.  
  176.    /* Copy control test string to allocated memory */
  177.       while ( ( *lpCtrlData++ = *Text++ ) );
  178.  
  179.    /* Copy extra byte count to allocated memory */
  180.       *lpCtrlData = ExtraBytes;
  181.  
  182.    /* Adjust memory offset past memory reallocated for dialog control */
  183.       wOffset += (WORD)( sizeof( DLGITEM ) + TextLength + sizeof(BYTE));
  184.  
  185.    /* Unlock reallocated memory */
  186.       GlobalUnlock( hDlgTemplate );
  187.  
  188.       iItems++;        /* bump up number of items in dialog */
  189.  
  190.       return( TRUE );  /* return successful */
  191. }
  192.  
  193. /* ------------- End Dialog Header ------------------------------ */
  194.  
  195. /*  
  196.  *  This routine changes the number of items in the
  197.  *  dialog header and then returns a handle to the global memory.
  198.  */
  199.  
  200. HANDLE FAR PASCAL EndDialogHeader()  /* end dialog header function */
  201. {
  202.   LPSTR    lpCtrlData;  /* Long pointer to locked dialog template memory  */
  203.  
  204.   lpCtrlData = GlobalLock( hDlgTemplate );
  205.   if (lpCtrlData == NULL) {
  206.     MessageBox ( GetFocus(), (LPSTR)"Global lock", (LPSTR)"Failed!", MB_OK);
  207.     GlobalFree( hDlgTemplate );
  208.   }
  209.  
  210.    /* 5th byte is address to # of items in dialog */
  211.   lpCtrlData++;
  212.   lpCtrlData++;
  213.   lpCtrlData++;
  214.   lpCtrlData++;
  215.  
  216.    /* set the number of items in control */
  217.   *lpCtrlData = (BYTE)iItems; 
  218.  
  219.   /* Unlock allocated memory */
  220.   GlobalUnlock( hDlgTemplate );
  221.  
  222.   /* Return the handle to the Dialog Template */
  223.   return (hDlgTemplate);
  224. }
  225.  
  226.  
  227. /* ------------- List of predefined dialog items ------------------- */
  228.  
  229. /*  
  230.  *  This routine defines a list of predefined dialog items.
  231.  *  This allows the user to just pass a number to get the 
  232.  *  attributes of a pre-defined control.
  233.  *  You can add to this list or modify this to best fit your needs.
  234.  *
  235.  *  Warning: If you change one of the styles, it could effect
  236.  *           other controls that expect the old behavior.
  237.  */
  238.  
  239. void SetStyleClass(iStyle)
  240. int  iStyle;
  241. {
  242.   /* if a iStyle is given then dtilControlClass will take on the default value. */ 
  243.  
  244.   switch ( iStyle ) {
  245.      case DI0:     /* check box */
  246.             DlgItem.dtilControlClass = BUTTONCLASS ;
  247.             DlgItem.dtilStyle = BS_CHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  248.             break;
  249.      case DI1:     /* icon */
  250.             DlgItem.dtilControlClass = STATICCLASS ;
  251.             DlgItem.dtilStyle = SS_ICON | WS_BORDER | WS_CHILD | WS_VISIBLE ;
  252.             break;
  253.      case DI2:     /* black box */
  254.             DlgItem.dtilControlClass = STATICCLASS ;
  255.             DlgItem.dtilStyle = SS_BLACKRECT | WS_CHILD | WS_VISIBLE ;
  256.             break;
  257.      case DI3:     /* rectangle */
  258.             DlgItem.dtilControlClass = STATICCLASS ;
  259.             DlgItem.dtilStyle = SS_BLACKFRAME | WS_CHILD | WS_VISIBLE ;
  260.             break;
  261.      case DI4:     /* left static text */
  262.             DlgItem.dtilControlClass = STATICCLASS ;
  263.             DlgItem.dtilStyle = SS_LEFT | WS_CHILD | WS_VISIBLE ;
  264.             break;
  265.      case DI5:     /* multiline edit box */
  266.             DlgItem.dtilControlClass = EDITCLASS ;
  267.             DlgItem.dtilStyle = ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
  268.                              ES_AUTOVSCROLL | ES_AUTOHSCROLL | 
  269.                              WS_VSCROLL | WS_HSCROLL | WS_BORDER | 
  270.                              WS_TABSTOP | WS_CHILD | WS_VISIBLE ; 
  271.             break;
  272.      case DI6:     /* list box - sorted */
  273.             DlgItem.dtilControlClass = LISTBOXCLASS ;
  274.             DlgItem.dtilStyle = LBS_STANDARD | WS_CHILD | WS_VISIBLE ;
  275.             break;
  276.      case DI7:     /* vertical scrollbar */
  277.             DlgItem.dtilControlClass = SCROLLBARCLASS ;
  278.             DlgItem.dtilStyle = SBS_VERT | WS_CHILD | WS_VISIBLE ;
  279.             break;
  280.      case DI8:     /* horizontal scrollbar */
  281.             DlgItem.dtilControlClass = SCROLLBARCLASS ;
  282.             DlgItem.dtilStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE ;
  283.             break;
  284.      case DI9:     /* group box */
  285.             DlgItem.dtilControlClass = BUTTONCLASS ;
  286.             DlgItem.dtilStyle = BS_GROUPBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  287.             break;
  288.      case DI10:    /* Push button */
  289.             DlgItem.dtilControlClass = BUTTONCLASS ;
  290.             DlgItem.dtilStyle = BS_PUSHBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  291.             break;
  292.      case DI11:    /* radio button */
  293.             DlgItem.dtilControlClass = BUTTONCLASS ;
  294.             DlgItem.dtilStyle = BS_RADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  295.             break;
  296.      case DI12:    /* default push button */
  297.             DlgItem.dtilControlClass = BUTTONCLASS ;
  298.             DlgItem.dtilStyle = BS_DEFPUSHBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  299.             break;
  300.      case DI13:    /* left check box */
  301.             DlgItem.dtilControlClass = BUTTONCLASS ;
  302.             DlgItem.dtilStyle = BS_LEFTTEXT | BS_CHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  303.             break;
  304.      case DI14:    /* 3 auto state button */
  305.             DlgItem.dtilControlClass = BUTTONCLASS ;
  306.             DlgItem.dtilStyle = BS_AUTO3STATE | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  307.             break;
  308.      case DI15:    /* centered edit control */
  309.             DlgItem.dtilControlClass = EDITCLASS ;
  310.             DlgItem.dtilStyle = ES_CENTER | ES_MULTILINE | WS_BORDER | WS_TABSTOP |
  311.                              WS_CHILD  | WS_VISIBLE ; 
  312.             break;
  313.      case DI16:    /* right edit control */
  314.             DlgItem.dtilControlClass = EDITCLASS ;
  315.             DlgItem.dtilStyle = ES_RIGHT | ES_MULTILINE | WS_BORDER | WS_TABSTOP | 
  316.                              WS_CHILD | WS_VISIBLE ; 
  317.             break;
  318.      case DI17:    /* left edit control */
  319.             DlgItem.dtilControlClass = EDITCLASS ;
  320.             DlgItem.dtilStyle = ES_LEFT  | WS_BORDER | WS_TABSTOP |
  321.                              WS_CHILD | WS_VISIBLE ; 
  322.             break;
  323.  
  324.      case DI18:    /* listbox w/out sort */
  325.             DlgItem.dtilControlClass = LISTBOXCLASS ;
  326.             DlgItem.dtilStyle = LBS_NOTIFY | WS_BORDER | WS_VSCROLL |
  327.                              WS_CHILD   | WS_VISIBLE ;
  328.             break;
  329.      case DI19:    /* center static text */
  330.             DlgItem.dtilControlClass = STATICCLASS ;
  331.             DlgItem.dtilStyle = SS_CENTER | WS_BORDER | WS_CHILD | WS_VISIBLE ;
  332.             break;
  333.      case DI20:    /* right static text */
  334.             DlgItem.dtilControlClass = STATICCLASS ;
  335.             DlgItem.dtilStyle = SS_RIGHT | WS_CHILD | WS_VISIBLE ;
  336.             break;
  337.      default:      /* left edit control */
  338.             DlgItem.dtilControlClass = STATICCLASS ;
  339.             DlgItem.dtilStyle = SS_LEFT | WS_CHILD | WS_VISIBLE ;
  340.             break;
  341.   } /* end switch */
  342.  
  343. }
  344.  
  345.  
  346. WORD  lstrlen( lpszString )
  347. LPSTR lpszString; /* String to check  */
  348. {
  349.    WORD Length; /* Length of string */
  350.  
  351.    for ( Length = 0; *lpszString++ != '\0'; Length++ );
  352.  
  353.    return( Length );
  354. }
  355.  
  356.