home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv3_5 / dialog / dlgbox.c next >
Encoding:
C/C++ Source or Header  |  1989-03-18  |  12.0 KB  |  342 lines

  1. /* DLGBOX.C -- version 1.00 Demonstrates how to use the
  2.  *            DialogBoxIndirect and CreateDialogIndirect functions.
  3.  */
  4.  
  5. #include <windows.h>
  6. #include "dlgbox.h"
  7.  
  8. /* List of functions used in this module. */
  9. BOOL FAR PASCAL InitDiabox ( HANDLE , HANDLE , int );
  10. LONG FAR PASCAL DlgboxWndProc  (HANDLE, unsigned, WORD, LONG );
  11. BOOL FAR PASCAL DialogBoxWindowProc (HANDLE, unsigned, WORD, LONG);
  12. BOOL             BuildDialog(HWND);
  13.  
  14. /* The following functions are found in the DLGTEMP module.
  15.  * They are used to build the dialog structure that will be
  16.  * passed to the DialogBoxIndirect or CreateDialogIndirect function.
  17.  */
  18.  
  19. /* -------- Functions defined in another module ----------- */
  20. extern BOOL     FAR PASCAL CreateDialogHeader(LONG,BYTE,int,int,int,int,LPSTR,LPSTR,LPSTR);
  21. extern BOOL     FAR PASCAL CreateDialogItem(int,LONG,BYTE,int,int,int,int,LPSTR,BYTE );
  22. extern HANDLE   FAR PASCAL EndDialogHeader( );
  23.  
  24. /* -------- Static variables ----------- */
  25. HANDLE   hInst;              /* handle to Instance             */
  26. FARPROC  lpDlgTest;          /* Dialog processing routine      */
  27. HWND     hDlgTest = NULL;    /* handle for modeless dialog box */
  28. BOOL     nModeless;          /* flag set by menu selected      */
  29.  
  30. HANDLE   hDlgTemp;    /* Handle to created dialog template       */
  31. LPSTR    lpDTemplate; /* Long pointer to created dialog template */
  32.  
  33. /* ------------------ WinMain ------------------------------ */
  34.  
  35. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  36. HANDLE  hInstance , hPrevInstance;
  37. LPSTR   lpszCmdLine;
  38. int     cmdShow;
  39. {
  40.    MSG  msg;
  41.    BOOL bResult; /* Modeless dialog message processed flag */
  42.  
  43.    if ( hPrevInstance )  /* do not allow more than one instance */
  44.       return FALSE;
  45.  
  46.    InitDiabox ( hInstance, hPrevInstance, cmdShow );
  47.  
  48.    while ( GetMessage((LPMSG)&msg, NULL, 0, 0 )) {
  49.       bResult = FALSE;
  50.  
  51.       /* this structure allows checking for multiple 
  52.         modeless dialog boxes */
  53.  
  54.       if ( hDlgTest != NULL )
  55.          bResult = IsDialogMessage( hDlgTest, (LPMSG)&msg );
  56.  
  57.       if ( !bResult ) {
  58.          TranslateMessage((LPMSG)&msg);
  59.          DispatchMessage((LPMSG)&msg);
  60.       }
  61.    }
  62.    return msg.wParam ;
  63. }
  64.  
  65. /* -------------- Initialization section ---------------------- */
  66.  
  67. BOOL FAR PASCAL InitDiabox( hInstance, hPrevInstance, cmdShow )
  68. HANDLE hInstance;
  69. HANDLE hPrevInstance;
  70. int    cmdShow;
  71. {
  72.    WNDCLASS wcDiaboxClass;
  73.    HWND     hWnd;
  74.  
  75.    wcDiaboxClass.lpszClassName    =    (LPSTR) "Diabox";
  76.    wcDiaboxClass.hInstance        =    hInstance;
  77.    wcDiaboxClass.lpfnWndProc    =    DlgboxWndProc;
  78.    wcDiaboxClass.hCursor        =    LoadCursor( NULL, IDC_ARROW );
  79.    wcDiaboxClass.hIcon        =    (HICON)LoadIcon( hInstance, "dlgicon");
  80.    wcDiaboxClass.lpszMenuName    =    (LPSTR)"dlgbox";   /* menu added  */
  81.    wcDiaboxClass.hbrBackground    =    GetStockObject( WHITE_BRUSH );
  82.    wcDiaboxClass.style        =    CS_HREDRAW | CS_VREDRAW;
  83.    wcDiaboxClass.cbClsExtra        =    0;
  84.    wcDiaboxClass.cbWndExtra        =    0;
  85.  
  86.    RegisterClass ( (LPWNDCLASS) &wcDiaboxClass );
  87.  
  88.    hWnd = CreateWindow( (LPSTR)"Diabox",
  89.                         (LPSTR)"DialogBox",
  90.                         WS_OVERLAPPEDWINDOW,
  91.                         CW_USEDEFAULT,
  92.                         CW_USEDEFAULT,
  93.                         CW_USEDEFAULT,
  94.                         CW_USEDEFAULT,
  95.                         (HWND)NULL,
  96.                         (HMENU)NULL,
  97.                         (HANDLE)hInstance,
  98.                         (LPSTR)NULL
  99.                       );
  100.  
  101.    hInst = hInstance;       /*  instance saved for dialog box  */
  102.  
  103.    ShowWindow( hWnd, cmdShow );
  104.    UpdateWindow( hWnd );
  105.  
  106.    return( TRUE );
  107. }
  108.  
  109. /* ------------ parent's window procedure ------------ */
  110.  
  111. LONG FAR PASCAL DlgboxWndProc( hWnd, message, wParam, lParam )
  112. HWND     hWnd;
  113. unsigned message;
  114. WORD     wParam;
  115. LONG     lParam;
  116. {
  117.    PAINTSTRUCT ps;
  118.    int   iResult;
  119.  
  120.    switch (message) {
  121.       case WM_COMMAND:
  122.          switch (wParam) {
  123.  
  124.            case IDM_MODAL:
  125.  
  126.               if ( hDlgTest == NULL ) { /* allow only one dialog up at a time */
  127.                 nModeless = FALSE;
  128.                 if ( BuildDialog(hWnd) == TRUE) {
  129.                   lpDlgTest = MakeProcInstance( (FARPROC) DialogBoxWindowProc, hInst );
  130.  
  131.                   if (lpDlgTest == NULL) {
  132.                     MessageBox( GetFocus(), "MakeProcInstance failed!", "ERROR", MB_OK );
  133.                     GlobalFree( hDlgTemp );
  134.                     return FALSE;
  135.                   }
  136.                   iResult = DialogBoxIndirect ( hInst, hDlgTemp, hWnd, lpDlgTest);
  137.                 }
  138.               }
  139.               break;
  140.  
  141.            case IDM_MODELESS:
  142.  
  143.               if ( hDlgTest == NULL ) { /* allow only one dialog up at a time */
  144.                 nModeless = TRUE;
  145.                 if ( BuildDialog(hWnd) == TRUE) {
  146.                   lpDTemplate = GlobalLock( hDlgTemp );
  147.                   if (lpDTemplate == NULL) {
  148.                     MessageBox( GetFocus(), "Could not lock memory.", "Diabox", MB_OK );
  149.                     GlobalFree( hDlgTemp );
  150.                     return FALSE;
  151.                   }
  152.                   lpDlgTest = MakeProcInstance( (FARPROC) DialogBoxWindowProc, hInst );
  153.  
  154.                   if (lpDlgTest == NULL) {
  155.                     MessageBox( GetFocus(), "MakeProcInstance failed!", "ERROR", MB_OK );
  156.                     GlobalFree( hDlgTemp );
  157.                     return FALSE;
  158.                   }
  159.                   hDlgTest = CreateDialogIndirect( hInst, lpDTemplate, hWnd, lpDlgTest );
  160.                 }  /* exit if BuildDialog didn't work */
  161.               }
  162.               break;
  163.  
  164.            default:
  165.               return DefWindowProc( hWnd, message, wParam, lParam );
  166.               break;
  167.          }
  168.  
  169.       case WM_PAINT:
  170.          BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  171.          EndPaint  ( hWnd, (LPPAINTSTRUCT)&ps );
  172.          break;
  173.  
  174.       case WM_DESTROY:
  175.          FreeProcInstance ( (FARPROC)lpDlgTest );
  176.          PostQuitMessage( 0 );
  177.          break;
  178.  
  179.       default:
  180.          return( DefWindowProc( hWnd, message, wParam, lParam ) );
  181.    }
  182.  
  183.    return( 0L );
  184. }
  185.  
  186. /* ------------- Build dialog routine --------------------------- */
  187. /*  
  188.  *  This routine passes all the necessary information to the
  189.  *  dialog functions in DLGTEMP.C that will build the dialog table.
  190.  */
  191.  
  192. BOOL BuildDialog(hWnd)
  193. HWND  hWnd;
  194. {
  195.    BOOL    bResult;
  196.    HCURSOR hOldCursor;      /* Handle to old mouse cursor  */
  197.  
  198.    /* Build the Dialog header */
  199.  
  200.    bResult = CreateDialogHeader(
  201.      WS_BORDER  | WS_CAPTION | WS_DLGFRAME | WS_VSCROLL | 
  202.      WS_HSCROLL | WS_SYSMENU | WS_GROUP    | WS_TABSTOP  | 
  203.      WS_SIZEBOX | WS_VISIBLE | WS_POPUP,    /* window style */
  204.      (BYTE)12, 24, 12, 180, 160,            /* coordinates of Dialog box.*/
  205.      "dlgtemp",                             /* menu        */
  206.      "",                                    /* class name  */
  207.      "A test Dialog Box" );                 /* Caption bar */
  208.  
  209.    if (bResult == FALSE) {
  210.      MessageBox( GetFocus(), "Not enough memory.", "Diabox",  MB_OK );
  211.      return FALSE;
  212.    }
  213.  
  214.    hOldCursor = SetCursor( LoadCursor(NULL, IDC_WAIT) );
  215.  
  216.    /* Each call to CreateDialogItem is an item to be placed inside the dialog box */
  217.  
  218.    /* This next comment line is the value that is being passed */
  219.    /* id,  style, class, x,   y, cx, cy,      text, extrabytes */
  220.  
  221.    CreateDialogItem(  IDCHECK,DI0,   0,  60,  90, 45, 12,  "&Ck box",0x00 );/* ck box   */
  222.    CreateDialogItem( 3001,    DI1,   0,  44, 107,  0,  0,  "dlgicon",0x00 );/* icon     */
  223.    CreateDialogItem( 3002,    DI2,   0,  73, 107, 16, 17,         "",0x00 );/* black box*/
  224.    CreateDialogItem( 3003,    DI3,   0,   6, 107, 25, 15,         "",0x00 );/* rect     */
  225.    CreateDialogItem( 3004,    DI4,   0,   6, 132, 40, 10,"L Justify",0x00 );/* lft text */
  226.    CreateDialogItem( 3005,    DI5,   0, 122,  64, 40, 38,   "M Edit",0x00 );/* muli edt */
  227.    CreateDialogItem( 3006,    DI6,   0,  60,   8, 54, 32,       "lb",0x00 );/* list box */
  228.    CreateDialogItem( 3007,    DI7,   0,  43,   8,  8, 34,      "v s",0x00 );/*vert scrl */
  229.    CreateDialogItem( 3008,    DI8,   0,   6,  47, 47,  8,      "h s",0x00 );/*horz scrl */
  230.    CreateDialogItem( 3009,    DI9,   0,   6,   8, 32, 36,    "group",0x00 );/* group box*/
  231.    CreateDialogItem( IDCANCEL,DI10,  0, 102, 112, 24, 14,    "&Push",0x00 );/*push but  */
  232.    CreateDialogItem( 3011,    DI11,  0,   6,  61, 45, 12,   "&radio",0x00 );/*radio but */
  233.    CreateDialogItem(     IDOK,DI12,  0, 136, 112, 24, 14,      "&Ok",0x00 );/*def buton */
  234.    CreateDialogItem( 3013,    DI13,  0,   6,  90, 44, 12,"&L Ck box",0x00 );/*left ckbox*/
  235.    CreateDialogItem( 3014,    DI14,  0,   6,  76, 30, 12,   "&3auto",0x00 );/*3 autoton */
  236.    CreateDialogItem( 3015,    DI15,  0, 122,  27, 40, 12,   "C Edit",0x00 );/*edit cent */
  237.    CreateDialogItem( 3016,    DI16,  0, 122,  44, 40, 12,   "R Edit",0x00 );/*rt edit   */
  238.    CreateDialogItem(  IDDEDIT,DI17,  0, 122,   8, 40, 12,   "L Edit",0x00 );/* L edit   */
  239.    CreateDialogItem( 3018,    DI18,  0,  60,  45, 54, 32,  "no sort",0x00 );/* lb w/out */
  240.    CreateDialogItem( 3019,    DI19,  0,  66, 132, 50, 10,   "Center",0x00 );/* CEN text */
  241.    CreateDialogItem( 3020,    DI20,  0, 122, 132, 46, 10,"R Justify",0x00 );/* RIG text */
  242.  
  243.    SetCursor( hOldCursor );
  244.  
  245.    /* Get the handle to the new dialog table */
  246.  
  247.    hDlgTemp = EndDialogHeader();  /* end dialog template, return hDlgTemp. */
  248.    if (hDlgTemp == NULL) {
  249.      GlobalFree(hDlgTemp);
  250.      MessageBox( GetFocus(), "End dialog routine.", "Error!", MB_OK );
  251.      return FALSE;
  252.    }
  253.    return TRUE;
  254. }
  255.  
  256. /* --------------  dialog box window procedure ------------ */
  257. /*  
  258.  *  This routine processes all the message for the dialog that
  259.  *  was dynamically created. 
  260.  */
  261.  
  262. BOOL FAR PASCAL DialogBoxWindowProc ( hDlg, message, wParam, lParam )
  263. HWND     hDlg;
  264. unsigned message;
  265. WORD     wParam;
  266. LONG     lParam;
  267. {
  268.    char    TempEdit[24];
  269.    HWND    hListBox, hListBoxNS;
  270.  
  271.    switch(message) {
  272.  
  273.       case WM_INITDIALOG:
  274.  
  275.          hListBox = GetDlgItem( hDlg, 3006 );
  276.          SendMessage(hListBox, LB_ADDSTRING, 0,(LONG)(LPSTR)"Sorted");
  277.          SendMessage(hListBox, LB_ADDSTRING, 0,(LONG)(LPSTR)"CCC");
  278.          SendMessage(hListBox, LB_ADDSTRING, 0,(LONG)(LPSTR)"AAA");
  279.          SendMessage(hListBox, LB_ADDSTRING, 0,(LONG)(LPSTR)"BBB");
  280.  
  281.          hListBoxNS = GetDlgItem( hDlg, 3018 );
  282.          SendMessage(hListBoxNS, LB_ADDSTRING, 0,(LONG)(LPSTR)"NOT Sorted!");
  283.          SendMessage(hListBoxNS, LB_ADDSTRING, 0,(LONG)(LPSTR)"CCC");
  284.          SendMessage(hListBoxNS, LB_ADDSTRING, 0,(LONG)(LPSTR)"AAA");
  285.          SendMessage(hListBoxNS, LB_ADDSTRING, 0,(LONG)(LPSTR)"BBB");
  286.  
  287.          SetFocus( GetDlgItem( hDlg, IDOK ) );
  288.          return(TRUE);
  289.  
  290.          break;
  291.  
  292.       case WM_COMMAND:
  293.  
  294.          switch( wParam ) {
  295.  
  296.             case IDCHECK: /*  Check Box      */
  297.                CheckDlgButton(hDlg, IDCHECK, !IsDlgButtonChecked(hDlg,IDCHECK));
  298.                break;
  299.  
  300.             case IDM_ONE: /*  One menu item  */
  301.  
  302.                MessageBox ( GetFocus(), (LPSTR)"One selected.", (LPSTR)"Menu item", MB_OK );
  303.                break;
  304.  
  305.             case IDOK:    /*  Ok button      */
  306.  
  307.                GetDlgItemText( hDlg, IDDEDIT, TempEdit, 24 );
  308.                MessageBox ( GetFocus(), (LPSTR)TempEdit, (LPSTR)"Edit:", MB_OK );
  309.                /* fall through to IDCANCEL */
  310.  
  311.             case IDCANCEL: /* Cancel push button */
  312.  
  313.                hDlgTest = NULL;
  314.                if ( nModeless == FALSE )
  315.                   EndDialog ( hDlg, TRUE );
  316.                else
  317.                   DestroyWindow( hDlg );
  318.                return( TRUE );
  319.  
  320.             default:
  321.                 return( TRUE );
  322.          }  /* end wParam switch */
  323.          break;   /* end wm_command */
  324.  
  325.       case WM_DESTROY:
  326.  
  327.          hDlgTest = NULL;
  328.  
  329.          while (GlobalFlags(hDlgTemp) & GMEM_LOCKCOUNT)
  330.               GlobalUnlock( hDlgTemp );
  331.          if (hDlgTemp)
  332.            GlobalFree( hDlgTemp );
  333.  
  334.          break;
  335.  
  336.       default:
  337.          return( FALSE );
  338.  
  339.    }  /* end message switch */
  340.    return( FALSE );
  341. }
  342.