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

  1. /*
  2.  *   GetDlgItemInt
  3.  *
  4.  *   This program demonstrates the use of the GetDlgItemInt function.
  5.  *
  6.  */
  7.  
  8. #include "windows.h"
  9. #include <stdio.h>
  10. #include "gdlgitmi.h"
  11.  
  12. long FAR PASCAL ItemIWndProc(HWND, unsigned, WORD, LONG);
  13.  
  14. HANDLE  hInst;        /* Instance handle for dialog box.   */
  15. FARPROC lpprocDialog; /* Long pointer to dialog procedure. */
  16. char    szBuff[80];   /* Buffer for edit control text.     */
  17.  
  18. BOOL FAR PASCAL DialogBoxProc(hDlg, message, wParam, lParam)
  19. HWND     hDlg;
  20. unsigned message;
  21. WORD     wParam;
  22. LONG     lParam;
  23. {
  24.   int  nNumber;      /* Integer from the edit control. */
  25.   BOOL bTranslated;  /* Translated flag pointer.       */
  26.  
  27.   switch (message)
  28.     {
  29.     case WM_INITDIALOG:
  30.       SetFocus(GetDlgItem(hDlg, EDIT_CONTROL));
  31.       return FALSE;
  32.  
  33.     case WM_COMMAND:
  34.       if (wParam == ID_OK)
  35.         {
  36.          /* Get the integer value of text entered. */
  37.         nNumber = (int)GetDlgItemInt(hDlg, EDIT_CONTROL,
  38.                                      (BOOL FAR *)&bTranslated,
  39.                                      TRUE);
  40.         if (bTranslated != FALSE)
  41.           {  /* Display that value in a message box if valid. */
  42.           sprintf(szBuff, "%s%d", "The number from the edit field: ", nNumber);
  43.           MessageBox(GetFocus(), (LPSTR)szBuff,
  44.                      (LPSTR)"GetDlgItemInt()", MB_OK);
  45.           }
  46.         else /* Or display an error. */
  47.           MessageBox(GetFocus(),
  48.                      (LPSTR)"Non-numeric character or exceeded max!",
  49.                      (LPSTR)"GetDlgItemInt() Error!",
  50.                      MB_OK | MB_ICONEXCLAMATION);
  51.         /* Quit the dialog box. */
  52.         EndDialog(hDlg, TRUE);
  53.         return TRUE;
  54.         }
  55.       else
  56.         return FALSE;
  57.  
  58.     default:
  59.       return FALSE;
  60.     }
  61. }
  62.  
  63. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  64. HANDLE hInstance, hPrevInstance;
  65. LPSTR  lpszCmdLine;
  66. int    cmdShow;
  67. {
  68.   MSG         msg;         /* Messages.               */
  69.   HWND        hWnd;        /* Window handle.          */
  70.   PWNDCLASS   pItemIClass; /* Window class structure. */
  71.  
  72.   pItemIClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  73.   pItemIClass->hIcon          = LoadIcon(hInstance,NULL);
  74.   pItemIClass->lpszMenuName   = (LPSTR)"getintmenu";
  75.   pItemIClass->lpszClassName  = (LPSTR)"gdlgitmi";
  76.   pItemIClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  77.   pItemIClass->hInstance      = hInstance;
  78.   pItemIClass->style          = CS_HREDRAW | CS_VREDRAW;
  79.   pItemIClass->lpfnWndProc    = ItemIWndProc;
  80.  
  81.   if (!RegisterClass((LPWNDCLASS)pItemIClass))
  82.     return FALSE;
  83.   LocalFree((HANDLE)pItemIClass);
  84.  
  85.   hWnd = CreateWindow((LPSTR)"gdlgitmi", (LPSTR)"GetDlgItemInt",
  86.                       WS_OVERLAPPEDWINDOW,
  87.                       CW_USEDEFAULT, CW_USEDEFAULT,
  88.                       CW_USEDEFAULT,  CW_USEDEFAULT,
  89.                       (HWND)NULL, (HMENU)NULL, (HANDLE)hInstance,
  90.                       (LPSTR)NULL);
  91.   hInst = hInstance;
  92.  
  93.   ShowWindow(hWnd, cmdShow);
  94.   UpdateWindow(hWnd);
  95.  
  96.   while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  97.     {
  98.     TranslateMessage((LPMSG)&msg);
  99.     DispatchMessage((LPMSG)&msg);
  100.     }
  101.  
  102.   return (int)msg.wParam;
  103. }
  104.  
  105.  
  106. /* Procedures which make up the window class. */
  107. long FAR PASCAL ItemIWndProc(hWnd, message, wParam, lParam)
  108. HWND hWnd;
  109. unsigned message;
  110. WORD wParam;
  111. LONG lParam;
  112. {
  113.   switch (message)
  114.     {
  115.     case WM_COMMAND:
  116.       switch (wParam)
  117.         {
  118.         case ID_INTBOX:
  119.           lpprocDialog = MakeProcInstance((FARPROC)DialogBoxProc, hInst);
  120.           DialogBox(hInst, MAKEINTRESOURCE(INTBOX), hWnd, lpprocDialog);
  121.           FreeProcInstance((FARPROC)lpprocDialog);
  122.           break;
  123.  
  124.         default:
  125.           return DefWindowProc(hWnd, message, wParam, lParam);
  126.           break;
  127.         }
  128.       break;
  129.  
  130.     case WM_DESTROY:
  131.       PostQuitMessage(0);
  132.       break;
  133.  
  134.     default:
  135.       return DefWindowProc(hWnd, message, wParam, lParam);
  136.       break;
  137.     }
  138.   return(0L);
  139. }
  140.  
  141.