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

  1. /*
  2.  *   GetDlgItem
  3.  *
  4.  *   This program demonstrates the use of the GetDlgItem function.
  5.  *
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include "gdlgitm.h"
  11.  
  12. long FAR PASCAL DlgItmWndProc(HWND, unsigned, WORD, LONG);
  13.  
  14. HANDLE  hInst;        /* Instance handle for dialog box. */
  15. FARPROC lpprocDialog; /* Pointer to dialog procedure.    */
  16.  
  17. BOOL FAR PASCAL DialogBoxProc(hDlg, message, wParam, lParam)
  18. HWND     hDlg;
  19. unsigned message;
  20. WORD     wParam;
  21. LONG     lParam;
  22.   switch (message)
  23.     {
  24.     case WM_INITDIALOG:
  25.       SetFocus(GetDlgItem(hDlg, EDIT_CONTROL));
  26.       return FALSE;
  27.  
  28.     case WM_COMMAND:
  29.       if (wParam == ID_OK)
  30.         { 
  31.         char szBuff[80]; /* Output buffer from the edit control. */
  32.         HWND hCtl;       /* Handle to the edit control, Used by  */
  33.                          /* GetDlgItem().                        */
  34.  
  35.         /* Get the handle to the edit control window. */
  36.         MessageBox(GetFocus(),
  37.                    (LPSTR)"Getting handle to control in dialog box",
  38.                    (LPSTR)"GetDlgItem", MB_OK);
  39.         hCtl = GetDlgItem(hDlg, EDIT_CONTROL);
  40.         if (hCtl == NULL)
  41.           MessageBox(GetFocus(), (LPSTR)"Error In Obatining Handle!",
  42.                      (LPSTR)"GetDlgItem() Error!",
  43.                      MB_OK | MB_ICONEXCLAMATION);
  44.         else
  45.           /* If the handle is good, get and show the edit control text. */
  46.           SendMessage(hCtl, WM_GETTEXT, (WORD)80, (LONG)szBuff);
  47.           MessageBox(GetFocus(), (LPSTR)szBuff,
  48.                      (LPSTR)"Edit Box Contents using GetDlgItem()",
  49.                      MB_OK);
  50.         /* And Quit. */
  51.         EndDialog(hDlg,TRUE);
  52.         return TRUE;
  53.         }
  54.       else return FALSE;
  55.  
  56.     default:
  57.       return FALSE;
  58.    } 
  59. }
  60.  
  61. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  62. HANDLE hInstance, hPrevInstance;
  63. LPSTR  lpszCmdLine;
  64. int    cmdShow;
  65. {
  66.   MSG       msg;          /* Window messages.           */
  67.   HWND      hWnd;         /* Window handle.             */
  68.   PWNDCLASS pDlgItmClass; /* Pointer to the class data. */
  69.  
  70.   pDlgItmClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  71.  
  72.   pDlgItmClass->hCursor       = LoadCursor(NULL, IDC_ARROW);
  73.   pDlgItmClass->hIcon         = LoadIcon(hInstance, NULL);
  74.   pDlgItmClass->lpszMenuName  = (LPSTR)"dlgmenu";
  75.   pDlgItmClass->lpszClassName = (LPSTR)"gdlgitm";
  76.   pDlgItmClass->hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  77.   pDlgItmClass->hInstance     = hInstance;
  78.   pDlgItmClass->style         = CS_HREDRAW | CS_VREDRAW;
  79.   pDlgItmClass->lpfnWndProc   = DlgItmWndProc;
  80.  
  81.   if (!RegisterClass((LPWNDCLASS)pDlgItmClass))
  82.     return FALSE;
  83.  
  84.   LocalFree((HANDLE)pDlgItmClass);
  85.  
  86.   hWnd = CreateWindow((LPSTR)"gdlgitm", (LPSTR)"GetDlgItem",
  87.                       WS_OVERLAPPEDWINDOW,
  88.                       CW_USEDEFAULT, CW_USEDEFAULT,
  89.                       CW_USEDEFAULT,  CW_USEDEFAULT,
  90.                       (HWND)NULL, (HMENU)NULL, (HANDLE)hInstance,
  91.                       (LPSTR)NULL);
  92.   hInst = hInstance;
  93.  
  94.   ShowWindow(hWnd, cmdShow);
  95.   UpdateWindow(hWnd);
  96.  
  97.   while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  98.     {
  99.     TranslateMessage((LPMSG)&msg);
  100.     DispatchMessage((LPMSG)&msg);
  101.     }
  102.  
  103.   return (int)msg.wParam;
  104. }
  105.  
  106. /* Procedures which make up the window class. */
  107. long FAR PASCAL DlgItmWndProc(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_DLGMENU:
  119.           /* Show the demo dialog box. */
  120.           lpprocDialog = MakeProcInstance((FARPROC)DialogBoxProc, hInst);
  121.           DialogBox(hInst, MAKEINTRESOURCE(ITEMBOX), hWnd, lpprocDialog);
  122.           FreeProcInstance((FARPROC)lpprocDialog);
  123.           break;
  124.  
  125.         default:
  126.           return DefWindowProc(hWnd, message, wParam, lParam);
  127.           break;
  128.         }
  129.       break;
  130.  
  131.     case WM_DESTROY:
  132.       PostQuitMessage(0);
  133.       break;
  134.  
  135.     default:
  136.       return DefWindowProc(hWnd, message, wParam, lParam);
  137.       break;
  138.     }
  139.   return(0L);
  140. }
  141.