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

  1. /*
  2.  *   GetDlgItemText
  3.  *
  4.  *   This program demonstrates the use of the GetDlgItemTxt function.
  5.  *   GetDlgItem text retrieves the caption of or the text associated
  6.  *   with a control in a dialog box. GetDlgItemTxt is called from
  7.  *   DialogBoxProc in this application.
  8.  *
  9.  */
  10.  
  11. #include "windows.h"
  12. #include "gdlgitmt.h"
  13.  
  14. long FAR PASCAL ItemTWndProc(HWND, unsigned, WORD, LONG);
  15.  
  16. HANDLE  hInst;        /* Instance handle.              */
  17. FARPROC lpprocDialog; /* Dialog procedure pointer.     */
  18.  
  19. BOOL FAR PASCAL DialogBoxProc(hDlg, message, wParam, lParam)
  20. HWND     hDlg;
  21. unsigned message;
  22. WORD     wParam;
  23. LONG     lParam;
  24. {
  25.   int  nNumChars;  /* Number of characters entered in the edit field. */
  26.   char szBuff[80]; /* Buffer for edit control text.                   */
  27.  
  28.   switch (message)
  29.     {
  30.     case WM_INITDIALOG:
  31.       SetFocus(GetDlgItem(hDlg, EDIT_CONTROL));
  32.       return FALSE;
  33.  
  34.     case WM_COMMAND:
  35.       if (wParam == ID_OK)
  36.         {
  37.         /* Get the text entered in the dialog box control. */
  38.         nNumChars = GetDlgItemText(hDlg, EDIT_CONTROL,
  39.                                    (LPSTR)szBuff, (int)80);
  40.         if (nNumChars == 0) /* If no text, say it. */
  41.           MessageBox(GetFocus(),
  42.                      (LPSTR)"No characters entered in edit field",
  43.                      (LPSTR)"GetDlgItemText()",
  44.                      MB_OK);
  45.         else /* Otherwise, show the text. */
  46.           MessageBox(GetFocus(),(LPSTR)szBuff,
  47.                      (LPSTR)"GetDlgItemText() Obtained:",
  48.                      MB_OK);
  49.         EndDialog(hDlg,TRUE);
  50.         return TRUE;
  51.         }
  52.       else
  53.         return FALSE;
  54.  
  55.     default:
  56.       return FALSE;
  57.     }
  58. }
  59.  
  60. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  61. HANDLE hInstance, hPrevInstance;
  62. LPSTR  lpszCmdLine;
  63. int    cmdShow;
  64. {
  65.   MSG       msg;         /* Message structure.      */
  66.   HWND      hWnd;        /* Window handle.          */
  67.   PWNDCLASS pItemTClass; /* Window class structure. */
  68.  
  69.   pItemTClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  70.  
  71.   pItemTClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  72.   pItemTClass->hIcon          = LoadIcon(hInstance,NULL);
  73.   pItemTClass->lpszMenuName   = (LPSTR)"textmenu";
  74.   pItemTClass->lpszClassName  = (LPSTR)"gdlgitmt";
  75.   pItemTClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  76.   pItemTClass->hInstance      = hInstance;
  77.   pItemTClass->style          = CS_HREDRAW | CS_VREDRAW;
  78.   pItemTClass->lpfnWndProc    = ItemTWndProc;
  79.  
  80.   if (!RegisterClass((LPWNDCLASS)pItemTClass))
  81.     return FALSE;
  82.   LocalFree((HANDLE)pItemTClass);
  83.  
  84.   hWnd = CreateWindow((LPSTR)"gdlgitmt",
  85.                       (LPSTR)"GetDlgItemText",
  86.                       WS_OVERLAPPEDWINDOW,
  87.                       CW_USEDEFAULT,
  88.                       CW_USEDEFAULT,
  89.                       CW_USEDEFAULT,
  90.                       CW_USEDEFAULT,
  91.                       (HWND)NULL,
  92.                       (HMENU)NULL,
  93.                       (HANDLE)hInstance,
  94.                       (LPSTR)NULL);
  95.   hInst = hInstance;
  96.  
  97.   ShowWindow(hWnd, cmdShow);
  98.   UpdateWindow(hWnd);
  99.  
  100.   while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  101.     {
  102.     TranslateMessage((LPMSG)&msg);
  103.     DispatchMessage((LPMSG)&msg);
  104.     }
  105.  
  106.   return (int)msg.wParam;
  107. }
  108.  
  109. long FAR PASCAL ItemTWndProc(hWnd, message, wParam, lParam)
  110. HWND     hWnd;
  111. unsigned message;
  112. WORD     wParam;
  113. LONG     lParam;
  114. {
  115.   switch (message)
  116.     {
  117.     case WM_COMMAND:
  118.       switch (wParam)
  119.         {
  120.         case ID_TEXTBOX:
  121.           lpprocDialog = MakeProcInstance((FARPROC)DialogBoxProc, hInst);
  122.           DialogBox(hInst, MAKEINTRESOURCE(TEXTBOX), hWnd, lpprocDialog);
  123.           FreeProcInstance((FARPROC)lpprocDialog);
  124.           break;
  125.  
  126.         default:
  127.           return DefWindowProc(hWnd, message, wParam, lParam);
  128.           break;
  129.         }
  130.       break;
  131.  
  132.     case WM_DESTROY:
  133.       PostQuitMessage(0);
  134.       break;
  135.  
  136.     default:
  137.       return DefWindowProc(hWnd, message, wParam, lParam);
  138.       break;
  139.     }
  140.   return(0L);
  141. }
  142.