home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d020_1_4 / 6.ddi / INPUT / INPUT.C next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  11.4 KB  |  377 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Input.c
  4.  
  5.     PURPOSE: Input template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15. ****************************************************************************/
  16.  
  17. #include "windows.h"
  18. #include "input.h"
  19.  
  20. HANDLE hInst;
  21.  
  22. char MouseText[48];                                   /* mouse state         */
  23. char ButtonText[48];                                  /* mouse-button state  */
  24. char KeyboardText[48];                                /* keyboard state      */
  25. char CharacterText[48];                               /* latest character    */
  26. char ScrollText[48];                                  /* scroll status       */
  27. char TimerText[48];                                   /* timer state         */
  28. RECT rectMouse;
  29. RECT rectButton;
  30. RECT rectKeyboard;
  31. RECT rectCharacter;
  32. RECT rectScroll;
  33. RECT rectTimer;
  34. int idTimer;                                          /* timer ID            */
  35. int nTimerCount = 0;                                  /* current timer count */
  36.  
  37. /****************************************************************************
  38.  
  39.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  40.  
  41.     PURPOSE: calls initialization function, processes message loop
  42.  
  43. ****************************************************************************/
  44.  
  45. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  46. HANDLE hInstance;
  47. HANDLE hPrevInstance;
  48. LPSTR lpCmdLine;
  49. int nCmdShow;
  50. {
  51.     MSG msg;
  52.  
  53.     if (!hPrevInstance)
  54.     if (!InitApplication(hInstance))
  55.         return (FALSE);
  56.  
  57.     if (!InitInstance(hInstance, nCmdShow))
  58.         return (FALSE);
  59.  
  60.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  61.     TranslateMessage(&msg);
  62.     DispatchMessage(&msg);
  63.     }
  64.     return (msg.wParam);
  65. }
  66.  
  67.  
  68. /****************************************************************************
  69.  
  70.     FUNCTION: InitApplication(HANDLE)
  71.  
  72.     PURPOSE: Initializes window data and registers window class
  73.  
  74. ****************************************************************************/
  75.  
  76. BOOL InitApplication(hInstance)
  77. HANDLE hInstance;
  78. {
  79.     WNDCLASS  wc;
  80.  
  81.     wc.style = CS_DBLCLKS;          /* double-click messages */
  82.     wc.lpfnWndProc = MainWndProc;
  83.     wc.cbClsExtra = 0;
  84.     wc.cbWndExtra = 0;
  85.     wc.hInstance = hInstance;
  86.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  87.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  88.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  89.     wc.lpszMenuName =  "InputMenu";
  90.     wc.lpszClassName = "InputWClass";
  91.  
  92.     return (RegisterClass(&wc));
  93. }
  94.  
  95.  
  96. /****************************************************************************
  97.  
  98.     FUNCTION:  InitInstance(HANDLE, int)
  99.  
  100.     PURPOSE:  Saves instance handle and creates main window
  101.  
  102. ****************************************************************************/
  103.  
  104. BOOL InitInstance(hInstance, nCmdShow)
  105.     HANDLE          hInstance;
  106.     int             nCmdShow;
  107. {
  108.     HWND            hWnd;
  109.     HDC             hDC;
  110.     TEXTMETRIC      textmetric;
  111.     RECT            rect;
  112.     int             nLineHeight;
  113.  
  114.  
  115.     hInst = hInstance;
  116.  
  117.     hWnd = CreateWindow(
  118.         "InputWClass",
  119.         "Input Sample Application",
  120.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,  /* horz & vert scroll bars */
  121.         CW_USEDEFAULT,
  122.         CW_USEDEFAULT,
  123.         CW_USEDEFAULT,
  124.         CW_USEDEFAULT,
  125.         NULL,
  126.         NULL,
  127.         hInstance,
  128.         NULL
  129.     );
  130.  
  131.     if (!hWnd)
  132.         return (FALSE);
  133.  
  134.  
  135.     hDC = GetDC(hWnd);
  136.     GetTextMetrics(hDC, &textmetric);
  137.     ReleaseDC(hWnd, hDC);
  138.     nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
  139.  
  140.     rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
  141.     rect.right  = GetDeviceCaps(hDC, HORZRES);
  142.     rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
  143.     rect.bottom = rect.top + nLineHeight;
  144.     rectMouse   = rect;
  145.  
  146.     rect.top += nLineHeight;
  147.     rect.bottom += nLineHeight;
  148.     rectButton = rect;
  149.  
  150.     rect.top += nLineHeight;
  151.     rect.bottom += nLineHeight;
  152.     rectKeyboard = rect;
  153.  
  154.     rect.top += nLineHeight;
  155.     rect.bottom += nLineHeight;
  156.     rectCharacter = rect;
  157.  
  158.     rect.top += nLineHeight;
  159.     rect.bottom += nLineHeight;
  160.     rectScroll = rect;
  161.  
  162.     rect.top += nLineHeight;
  163.     rect.bottom += nLineHeight;
  164.     rectTimer = rect;
  165.     
  166.     ShowWindow(hWnd, nCmdShow);
  167.     UpdateWindow(hWnd);
  168.     return (TRUE);
  169.  
  170. }
  171.  
  172. /****************************************************************************
  173.  
  174.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  175.  
  176.     PURPOSE:  Processes messages
  177.  
  178.     MESSAGES:
  179.  
  180.     WM_COMMAND    - application menu (About dialog box)
  181.     WM_CREATE     - create window
  182.         WM_MOUSEMOVE  - mouse movement
  183.         WM_LBUTTONDOWN - left mouse button pressed
  184.         WM_LBUTTONUP  - left mouse button released
  185.         WM_LBUTTONDBLCLK - left mouse button double clicked
  186.         WM_KEYDOWN    - key pressed
  187.         WM_KEYUP      - key released
  188.         WM_CHAR       - ASCII character received
  189.         WM_TIMER      - timer has elapsed
  190.         WM_HSCROLL    - mouse click in horizontal scroll bar
  191.         WM_VSCROLL    - mouse click in vertical scroll bar
  192.         WM_PAINT      - update window, draw objects
  193.         WM_DESTROY    - destroy window
  194.  
  195.     COMMENTS:
  196.  
  197.         This demonstrates how input messages are received, and what the
  198.         additional information is that comes with the message.
  199.  
  200. ****************************************************************************/
  201.  
  202. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  203. HWND hWnd;
  204. unsigned message;
  205. WORD wParam;
  206. LONG lParam;
  207. {
  208.     FARPROC lpProcAbout;
  209.  
  210.     HDC hDC;                         /* display-context variable     */
  211.     PAINTSTRUCT ps;                  /* paint structure              */
  212.     char HorzOrVertText[12];
  213.     char ScrollTypeText[20];
  214.     RECT rect;
  215.  
  216.     switch (message) {
  217.     case WM_COMMAND:
  218.         if (wParam == IDM_ABOUT) {
  219.         lpProcAbout = MakeProcInstance(About, hInst);
  220.  
  221.         DialogBox(hInst,
  222.             "AboutBox",
  223.             hWnd,
  224.             lpProcAbout);
  225.  
  226.         FreeProcInstance(lpProcAbout);
  227.         break;
  228.         }
  229.         else
  230.         return (DefWindowProc(hWnd, message, wParam, lParam));
  231.  
  232.         case WM_CREATE:
  233.  
  234.             /* Set the timer for five-second intervals */
  235.  
  236.             idTimer =  SetTimer(hWnd, NULL, 5000, (FARPROC) NULL);
  237.  
  238.             break;
  239.  
  240.         case WM_MOUSEMOVE:
  241.             wsprintf(MouseText, "WM_MOUSEMOVE: %x, %d, %d",
  242.                 wParam, LOWORD(lParam), HIWORD(lParam));
  243.             InvalidateRect(hWnd, &rectMouse, TRUE);
  244.             break;
  245.  
  246.         case WM_LBUTTONDOWN:
  247.             wsprintf(ButtonText, "WM_LBUTTONDOWN: %x, %d, %d",
  248.                 wParam, LOWORD(lParam), HIWORD(lParam));
  249.             InvalidateRect(hWnd, &rectButton, TRUE);
  250.             break;
  251.  
  252.         case WM_LBUTTONUP:
  253.             wsprintf(ButtonText, "WM_LBUTTONUP: %x, %d, %d",
  254.                 wParam, LOWORD(lParam), HIWORD(lParam));
  255.             InvalidateRect(hWnd, &rectButton, TRUE);
  256.             break;
  257.  
  258.         case WM_LBUTTONDBLCLK:
  259.             wsprintf(ButtonText, "WM_LBUTTONDBLCLK: %x, %d, %d",
  260.                 wParam, LOWORD(lParam), HIWORD(lParam));
  261.             InvalidateRect(hWnd, &rectButton, TRUE);
  262.             break;
  263.  
  264.         case WM_KEYDOWN:
  265.             wsprintf(KeyboardText, "WM_KEYDOWN: %x, %x, %x",
  266.                 wParam, LOWORD(lParam), HIWORD(lParam));
  267.             InvalidateRect(hWnd, &rectKeyboard, TRUE);
  268.             break;
  269.  
  270.         case WM_KEYUP:
  271.             wsprintf(KeyboardText, "WM_KEYUP: %x, %x, %x",
  272.                 wParam, LOWORD(lParam), HIWORD(lParam));
  273.             InvalidateRect(hWnd, &rectKeyboard, TRUE);
  274.             break;
  275.  
  276.         case WM_CHAR:
  277.             wsprintf(CharacterText, "WM_CHAR: %c, %x, %x",
  278.                 wParam, LOWORD(lParam), HIWORD(lParam));
  279.             InvalidateRect(hWnd, &rectCharacter, TRUE);
  280.             break;
  281.  
  282.         case WM_TIMER:
  283.             wsprintf(TimerText, "WM_TIMER: %d seconds",
  284.                 nTimerCount += 5);
  285.             InvalidateRect(hWnd, &rectTimer, TRUE);
  286.             break;
  287.  
  288.         case WM_HSCROLL:
  289.         case WM_VSCROLL:
  290.             strcpy(HorzOrVertText,
  291.                 (message == WM_HSCROLL) ? "WM_HSCROLL" : "WM_VSCROLL");
  292.             strcpy(ScrollTypeText,
  293.                 (wParam == SB_LINEUP) ? "SB_LINEUP" :
  294.                 (wParam == SB_LINEDOWN) ? "SB_LINEDOWN" :
  295.                 (wParam == SB_PAGEUP) ? "SB_PAGEUP" :
  296.                 (wParam == SB_PAGEDOWN) ? "SB_PAGEDOWN" :
  297.                 (wParam == SB_THUMBPOSITION) ? "SB_THUMBPOSITION" :
  298.                 (wParam == SB_THUMBTRACK) ? "SB_THUMBTRACK" :
  299.                 (wParam == SB_ENDSCROLL) ? "SB_ENDSCROLL" : "unknown");
  300.             wsprintf(ScrollText, "%s: %s, %x, %x",
  301.                 (LPSTR)HorzOrVertText,
  302.                 (LPSTR)ScrollTypeText,
  303.                 LOWORD(lParam),
  304.                 HIWORD(lParam));
  305.             InvalidateRect(hWnd, &rectScroll, TRUE);
  306.             break;
  307.  
  308.         case WM_PAINT:
  309.             hDC = BeginPaint (hWnd, &ps);
  310.  
  311.             if (IntersectRect(&rect, &rectMouse, &ps.rcPaint))
  312.                 TextOut(hDC, rectMouse.left, rectMouse.top, 
  313.                         MouseText, strlen(MouseText));
  314.             if (IntersectRect(&rect, &rectButton, &ps.rcPaint))
  315.                 TextOut(hDC, rectButton.left, rectButton.top, 
  316.                         ButtonText, strlen(ButtonText));
  317.             if (IntersectRect(&rect, &rectKeyboard, &ps.rcPaint))
  318.                 TextOut(hDC, rectKeyboard.left, rectKeyboard.top, 
  319.                         KeyboardText, strlen(KeyboardText));
  320.             if (IntersectRect(&rect, &rectCharacter, &ps.rcPaint))
  321.                 TextOut(hDC, rectCharacter.left, rectCharacter.top, 
  322.                         CharacterText, strlen(CharacterText));
  323.             if (IntersectRect(&rect, &rectTimer, &ps.rcPaint))
  324.                 TextOut(hDC, rectTimer.left, rectTimer.top, 
  325.                         TimerText, strlen(TimerText));
  326.             if (IntersectRect(&rect, &rectScroll, &ps.rcPaint))
  327.                 TextOut(hDC, rectScroll.left, rectScroll.top, 
  328.                         ScrollText, strlen(ScrollText));
  329.  
  330.             EndPaint(hWnd, &ps);
  331.             break;
  332.  
  333.         case WM_DESTROY:
  334.             KillTimer(hWnd, idTimer);                     /* Stops the timer */
  335.             PostQuitMessage(0);
  336.             break;
  337.  
  338.     default:
  339.         return (DefWindowProc(hWnd, message, wParam, lParam));
  340.     }
  341.     return (NULL);
  342. }
  343.  
  344.  
  345. /****************************************************************************
  346.  
  347.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  348.  
  349.     PURPOSE:  Processes messages for "About" dialog box
  350.  
  351.     MESSAGES:
  352.  
  353.     WM_INITDIALOG - initialize dialog box
  354.     WM_COMMAND    - Input received
  355.  
  356. ****************************************************************************/
  357.  
  358. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  359. HWND hDlg;
  360. unsigned message;
  361. WORD wParam;
  362. LONG lParam;
  363. {
  364.     switch (message) {
  365.     case WM_INITDIALOG:
  366.         return (TRUE);
  367.  
  368.     case WM_COMMAND:
  369.         if (wParam == IDOK) {
  370.         EndDialog(hDlg, TRUE);
  371.         return (TRUE);
  372.         }
  373.         break;
  374.     }
  375.     return (FALSE);
  376. }
  377.