home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / HANDLER / HANDTEST.C_ / HANDTEST.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  5.2 KB  |  180 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Handtest.c
  4.  
  5.     PURPOSE: Demonstrates the use of the HANDLER.DLL
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     DemoInit() - initializes window data and registers window
  11.     DemoWndProc() - processes messages
  12.     About() - processes messages for "About" dialog box
  13.  
  14.  
  15. ****************************************************************************/
  16. #include "windows.h"
  17. #include "handtest.h"
  18. #include "handler.h"
  19.  
  20. HANDLE hInst;
  21.  
  22.  
  23. int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  24. HANDLE hInstance;
  25. HANDLE hPrevInstance;
  26. LPSTR lpCmdLine;
  27. int nCmdShow;
  28. {
  29.      HWND hWnd;
  30.      MSG msg;
  31.  
  32.      if (!hPrevInstance)
  33.           if (!DemoInit (hInstance))
  34.                return (NULL);
  35.      hInst = hInstance;
  36.      hWnd = CreateWindow ("HandlerDemo", "Handler Demonstration",
  37.                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           500, 200, NULL, NULL, hInstance, NULL);
  39.      if (!hWnd)
  40.           return (NULL);
  41.      ShowWindow (hWnd, nCmdShow);
  42.      UpdateWindow (hWnd);
  43.      while (GetMessage (&msg, NULL, NULL, NULL)) {
  44.           TranslateMessage (&msg);
  45.           DispatchMessage (&msg);
  46.      }
  47.      return (msg.wParam);
  48. }
  49.  
  50. /****************************************************************************
  51.  
  52.     FUNCTION: DemoInit(HANDLE)
  53.  
  54.     PURPOSE: Initializes window data and registers window class
  55.  
  56. ****************************************************************************/
  57.  
  58.  
  59. BOOL DemoInit (hInstance)
  60. HANDLE hInstance;
  61. {
  62.      HANDLE hMemory;
  63.      PWNDCLASS pWndClass;
  64.      BOOL bSuccess;
  65.  
  66.      hMemory = LocalAlloc (LPTR, sizeof (WNDCLASS));
  67.      pWndClass = (PWNDCLASS) LocalLock (hMemory);
  68.      pWndClass->hCursor = LoadCursor (NULL, IDC_ARROW);
  69.      pWndClass->hIcon = LoadIcon (NULL, IDI_APPLICATION);
  70.      pWndClass->lpszMenuName = (LPSTR) "Menu";
  71.      pWndClass->lpszClassName = (LPSTR) "HandlerDemo";
  72.      pWndClass->hbrBackground = COLOR_WINDOW+1;
  73.      pWndClass->hInstance = hInstance;
  74.      pWndClass->style = NULL;
  75.      pWndClass->lpfnWndProc = DemoWndProc;
  76.      bSuccess = RegisterClass (pWndClass);
  77.      LocalUnlock (hMemory);
  78.      LocalFree (hMemory);
  79.      return (bSuccess);
  80. }
  81.  
  82. /****************************************************************************
  83.  
  84.     FUNCTION: DemoWndProc(HWND, UINT, WPARAM, LPARAM)
  85.  
  86.     PURPOSE:  Processes messages
  87.  
  88.  
  89. ****************************************************************************/
  90.  
  91.  
  92. long FAR PASCAL __export DemoWndProc (hWnd, message, wParam, lParam)
  93. HWND hWnd;
  94. UINT message;
  95. WPARAM wParam;
  96. LPARAM lParam;
  97. {
  98.      FARPROC lpProcAbout;
  99.      int nCount;
  100.      HDC hDC;
  101.      TEXTMETRIC tm;
  102.      static short cxChar, cyChar;
  103.      char szBuffer[80];
  104.  
  105.      switch (message) {
  106.      case WM_CREATE:                     /* Register Window handle   */
  107.           SetISRWindow (hWnd);             /* with handler DLL         */
  108.           SetTimer (hWnd, 1, 2000, NULL);     /* Set a time-out for display */
  109.           hDC = GetDC (hWnd);
  110.           GetTextMetrics (hDC, &tm);
  111.           cxChar = tm.tmAveCharWidth;
  112.           cyChar = tm.tmHeight + tm.tmExternalLeading;
  113.           ReleaseDC (hWnd, hDC);
  114.           break;
  115.      case ISRM_RUPT:                     /* Got a message from HANDLER */
  116.           hDC = GetDC (hWnd);
  117.           nCount = wParam;
  118.       TextOut (hDC, cxChar, cyChar * 2, szBuffer, wsprintf (szBuffer,
  119.                    "Total interrupts = %d", nCount));
  120.           ReleaseDC (hWnd, hDC);
  121.           break;
  122.      case WM_TIMER:                      /* Update display occasionally */
  123.           hDC = GetDC (hWnd);
  124.           nCount = GetISRCount ();
  125.       TextOut (hDC, cxChar, cyChar * 2, szBuffer, wsprintf (szBuffer,
  126.                    "Total interrupts = %d", nCount));
  127.           ReleaseDC (hWnd, hDC);
  128.           break;
  129.      case WM_COMMAND:
  130.           switch (wParam) {
  131.           case IDM_ABOUT:
  132.                lpProcAbout = MakeProcInstance (About, hInst);
  133.                DialogBox (hInst, "AboutBox", hWnd, lpProcAbout);
  134.                FreeProcInstance (lpProcAbout);
  135.                break;
  136.           }
  137.           break;
  138.      case WM_DESTROY:
  139.           KillTimer (hWnd, 1);
  140.           PostQuitMessage (NULL);
  141.           break;
  142.      default:
  143.           return (DefWindowProc (hWnd, message, wParam, lParam));
  144.      }
  145.      return (NULL);
  146. }
  147.  
  148. /****************************************************************************
  149.  
  150.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  151.  
  152.     PURPOSE:  Processes messages for "About" dialog box
  153.  
  154.     MESSAGES:
  155.  
  156.     WM_INITDIALOG - initialize dialog box
  157.     WM_COMMAND    - Input received
  158.  
  159. ****************************************************************************/
  160.  
  161.  
  162. BOOL FAR PASCAL __export About (hDlg, message, wParam, lParam)
  163. HWND hDlg;
  164. unsigned message;
  165. WORD wParam;
  166. LONG lParam;
  167. {
  168.      switch (message) {
  169.      case WM_INITDIALOG:
  170.           return (TRUE);
  171.      case WM_COMMAND:
  172.           if (wParam == IDOK || wParam == IDCANCEL) {
  173.                EndDialog (hDlg, TRUE);
  174.                return (TRUE);
  175.           }
  176.           return (TRUE);
  177.      }
  178.      return (FALSE);
  179. }
  180.