home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / dialog / senddlg.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  10KB  |  238 lines

  1. /*
  2.  *  SendDlgItemMessage
  3.  *  senddlg.c,
  4.  *
  5.  *  This program will open up a Dialog box with an edit conrol. When the
  6.  *  user hits the ADD button, the program will send the LISTBOX a 
  7.  *  LB_ADDSTRING message to along with appropriate parameters to add the
  8.  *  string in the edit control to the listbox.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"      /* required for all Windows applications */
  13. #include "senddlg.h"      /* specific to this program              */
  14.  
  15. HANDLE hInst;             /* current instance                      */
  16.  
  17. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  18. HANDLE hInstance;         /* current instance             */
  19. HANDLE hPrevInstance;     /* previous instance            */
  20. LPSTR lpCmdLine;          /* command line                 */
  21. int nCmdShow;             /* show-window type (open/icon) */
  22. {
  23.     HWND hWnd;            /* window handle       */
  24.     MSG msg;              /* message             */
  25.     HANDLE hMenu;         /* Handle to the menu  */
  26.  
  27.     if (!hPrevInstance)              /* Has application been initialized? */
  28.         if (!SendDlgInit(hInstance))
  29.             return (NULL);           /* Exits if unable to initialize     */
  30.  
  31.     hInst = hInstance;               /* Saves the current instance        */
  32.  
  33.     hMenu = LoadMenu( hInst, (LPSTR)"SendDlgMenu" );
  34.                                      /*  Load the menu    */
  35.  
  36.     hWnd = CreateWindow("SendDlg",   /* window class      */
  37.         "SendDlg Sample Application",  /* window name       */
  38.         WS_OVERLAPPEDWINDOW,         /* window style      */
  39.         CW_USEDEFAULT,               /* x position        */
  40.         CW_USEDEFAULT,               /* y position        */
  41.         CW_USEDEFAULT,               /* width             */
  42.         CW_USEDEFAULT,               /* height            */
  43.         NULL,                        /* parent handle     */
  44.         hMenu,                       /* menu or child ID  */
  45.         hInstance,                   /* instance          */
  46.         NULL);                       /* additional info   */
  47.  
  48.     if (!hWnd)                       /* Was the window created? */
  49.         return (NULL);
  50.  
  51.     ShowWindow(hWnd, nCmdShow);      /* Shows the window        */
  52.     UpdateWindow(hWnd);              /* Sends WM_PAINT message  */
  53.  
  54.     while (GetMessage(&msg,     /* message structure                      */
  55.             NULL,               /* handle of window receiving the message */
  56.             NULL,               /* lowest message to examine              */
  57.             NULL))              /* highest message to examine             */
  58.         {
  59.         TranslateMessage(&msg);  /* Translates virtual key codes           */
  60.         DispatchMessage(&msg);   /* Dispatches message to window           */
  61.     }
  62.     return (msg.wParam);         /* Returns the value from PostQuitMessage */
  63. }
  64.  
  65. BOOL SendDlgInit(hInstance)
  66. HANDLE hInstance;                /* current instance           */
  67. {
  68.     HANDLE hMemory;              /* handle to allocated memory */
  69.     PWNDCLASS pWndClass;         /* structure pointer          */
  70.     BOOL bSuccess;               /* RegisterClass() result     */
  71.  
  72.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  73.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  74.  
  75.     pWndClass->style = NULL;
  76.     pWndClass->lpfnWndProc = SendDlgWndProc;
  77.     pWndClass->hInstance = hInstance;
  78.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  79.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  80.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  81.     pWndClass->lpszMenuName = (LPSTR) "SendDlgMenu";
  82.     pWndClass->lpszClassName = (LPSTR) "SendDlg";
  83.  
  84.     bSuccess = RegisterClass(pWndClass);
  85.  
  86.     LocalUnlock(hMemory);     /* Unlocks the memory    */
  87.     LocalFree(hMemory);       /* Returns it to Windows */
  88.  
  89.     return (bSuccess);        /* Returns result of registering the window */
  90. }
  91.  
  92. long FAR PASCAL SendDlgWndProc(hWnd, message, wParam, lParam)
  93. HWND hWnd;                    /* window handle                   */
  94. unsigned message;             /* type of message                 */
  95. WORD wParam;                  /* additional information          */
  96. LONG lParam;                  /* additional information          */
  97. {
  98.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  99.     HMENU hMenu;              /* handle to the System menu       */
  100.  
  101.     switch (message) {
  102.         case WM_SYSCOMMAND:   /* message: command from system menu */
  103.             if (wParam == ID_ABOUT) {
  104.                 lpProcAbout = MakeProcInstance(About, hInst);
  105.  
  106.                 DialogBox(hInst,         /* current instance         */
  107.                     "AboutBox",          /* resource to use          */
  108.                     hWnd,                /* parent handle            */
  109.                     lpProcAbout);        /* About() instance address */
  110.  
  111.                 FreeProcInstance(lpProcAbout);
  112.                 break;
  113.             }
  114.  
  115.             else                         /* Lets Windows process it       */
  116.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  117.  
  118.         case WM_CREATE:                  /* message: window being created */
  119.  
  120.             /* Get the handle of the System menu */
  121.  
  122.             hMenu = GetSystemMenu(hWnd, FALSE);
  123.  
  124.             /* Add a separator to the menu */
  125.  
  126.             ChangeMenu(hMenu,                    /* menu handle         */
  127.                 NULL,                            /* menu item to change */
  128.                 NULL,                            /* new menu item       */
  129.                 NULL,                            /* menu identifier     */
  130.                 MF_APPEND | MF_SEPARATOR);       /* type of change      */
  131.  
  132.             /* Add new menu item to the System menu */
  133.  
  134.             ChangeMenu(hMenu,                    /* menu handle         */
  135.                 NULL,                            /* menu item to change */
  136.                 "A&bout SendDlg...",             /* new menu item       */
  137.                 ID_ABOUT,                        /* menu identifier     */
  138.                 MF_APPEND | MF_STRING);          /* type of change      */
  139.             break;
  140.  
  141.         case WM_COMMAND:
  142.             if ( wParam == IDM_DIALOG )
  143.               {
  144.               FARPROC    lpProcLineList;
  145.  
  146.               lpProcLineList = MakeProcInstance( LineList, hInst );
  147.  
  148.               DialogBox( hInst,             /*  Current Instance  */
  149.                          "ListIn",          /*  Resource to use  */
  150.                          hWnd,              /*  Parent Handle  */
  151.                          lpProcLineList );
  152.               FreeProcInstance( lpProcLineList );
  153.               break;
  154.               }
  155.  
  156.         case WM_DESTROY:             /* message: window being destroyed */
  157.             PostQuitMessage(0);
  158.             break;
  159.  
  160.         default:                     /* Passes it on if unproccessed    */
  161.             return (DefWindowProc(hWnd, message, wParam, lParam));
  162.     }
  163.     return (NULL);
  164. }
  165.  
  166. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  167. HWND hDlg;
  168. unsigned message;
  169. WORD wParam;
  170. LONG lParam;
  171. {
  172.     switch (message) {
  173.         case WM_INITDIALOG:              /* message: initialize dialog box */
  174.             return (TRUE);
  175.  
  176.         case WM_COMMAND:                 /* message: received a command */
  177.             if (wParam == IDOK) {        /* "OK" box selected?          */
  178.                 EndDialog(hDlg, NULL);   /* Exits the dialog box        */
  179.                 return (TRUE);
  180.             }
  181.             break;
  182.     }
  183.     return (FALSE);                      /* Didn't process a message    */
  184. }
  185.  
  186. BOOL FAR PASCAL LineList(hDlg, message, wParam, lParam)
  187. HWND hDlg;
  188. unsigned message;
  189. WORD wParam;
  190. LONG lParam;
  191. {
  192.     char  szBuffer[ 50 ];    /*  Buffer to hold text from edit control  */
  193.  
  194.     switch (message) {
  195.         case WM_INITDIALOG:              /* message: initialize dialog box */
  196.             return (TRUE);
  197.  
  198.         case WM_COMMAND:                 /* message: received a command */
  199.             if (wParam == ID_ADD )       /* "ADD" box selected?         */
  200.               {
  201.               GetDlgItemText( hDlg,      /*  Handle to the dialog box  */
  202.                               ID_EDIT,   /*  ID of item to get text from  */
  203.                               (LPSTR)szBuffer,  /*  Pointer to buffer  */
  204.                               50 );      /*  Maximum size of buffer  */
  205.               SendDlgItemMessage( hDlg,  /*  Handle to the dialog box  */
  206.                                   ID_LISTBOX,  /*  ID for item to send the
  207.                                                 *  message to  */
  208.                                   LB_ADDSTRING,  /*  Message to send  */
  209.                                   0,     /*  The wParam parameter  */
  210.                                   (LONG)(LPSTR)szBuffer );  /*  lParam  */
  211.               SetDlgItemText( hDlg,         /*  Handle to the dialog box  */
  212.                               ID_EDIT,      /*  ID for the edit control  */
  213.                               (LPSTR)"" );  /*  Text to put in edit control */
  214.               SetFocus( GetDlgItem( hDlg, ID_EDIT ) );
  215.                              /*  Give the focus to the edit control  */
  216. /*  The SendDlgItemMessage sends a message to the Listbox to add the
  217.  *  string to the listbox.  After that we set the text in the edit control
  218.  *  to an empty string, effectively clearing the edit control.  Then 
  219.  *  finally we set the focus to the edit control.  */
  220.  
  221.               return (TRUE);      /*  We processed a message  */
  222.               }
  223.             if ( wParam == ID_OK )
  224.               {
  225.               EndDialog(hDlg, NULL);   /* Exits the dialog box        */
  226.               return (TRUE);
  227.               }
  228.             if ( wParam == ID_CANCEL )
  229.               {
  230.               EndDialog(hDlg, NULL);   /* Exits the dialog box        */
  231.               return (TRUE);
  232.               }
  233.             break;
  234.  
  235.     }
  236.     return (FALSE);                      /* Didn't process a message    */
  237. }
  238.