home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c045 / 5.ddi / INPUT / INPUT.C$ / INPUT.bin
Encoding:
Text File  |  1992-01-01  |  10.1 KB  |  379 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 <string.h>
  19. #include "input.h"
  20.  
  21.  
  22. HANDLE hInst;
  23.  
  24. char MouseText[48];                                   /* mouse state         */
  25. char ButtonText[48];                                  /* mouse-button state  */
  26. char KeyboardText[48];                                /* keyboard state      */
  27. char CharacterText[48];                               /* latest character    */
  28. char ScrollText[48];                                  /* scroll status       */
  29. char TimerText[48];                                   /* timer state         */
  30. RECT rectMouse;
  31. RECT rectButton;
  32. RECT rectKeyboard;
  33. RECT rectCharacter;
  34. RECT rectScroll;
  35. RECT rectTimer;
  36. int idTimer;                                          /* timer ID            */
  37. int nTimerCount = 0;                                  /* current timer count */
  38.  
  39. /****************************************************************************
  40.  
  41.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  42.  
  43.     PURPOSE: calls initialization function, processes message loop
  44.  
  45. ****************************************************************************/
  46.  
  47. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  48. HANDLE hInstance;
  49. HANDLE hPrevInstance;
  50. LPSTR lpCmdLine;
  51. int nCmdShow;
  52. {
  53.     MSG msg;
  54.  
  55.     if (!hPrevInstance)
  56.         if (!InitApplication(hInstance))
  57.             return (FALSE);
  58.  
  59.     if (!InitInstance(hInstance, nCmdShow))
  60.         return (FALSE);
  61.  
  62.     while (GetMessage(&msg, NULL, NULL, NULL))
  63.     {
  64.         TranslateMessage(&msg);
  65.         DispatchMessage(&msg);
  66.     }
  67.     return (msg.wParam);
  68. }
  69.  
  70.  
  71. /****************************************************************************
  72.  
  73.     FUNCTION: InitApplication(HANDLE)
  74.  
  75.     PURPOSE: Initializes window data and registers window class
  76.  
  77. ****************************************************************************/
  78.  
  79. BOOL InitApplication(hInstance)
  80. HANDLE hInstance;
  81. {
  82.     WNDCLASS  wc;
  83.  
  84.     wc.style = CS_DBLCLKS;          /* double-click messages */
  85.     wc.lpfnWndProc = MainWndProc;
  86.     wc.cbClsExtra = 0;
  87.     wc.cbWndExtra = 0;
  88.     wc.hInstance = hInstance;
  89.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  90.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  91.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  92.     wc.lpszMenuName =  "InputMenu";
  93.     wc.lpszClassName = "InputWClass";
  94.  
  95.     return (RegisterClass(&wc));
  96. }
  97.  
  98.  
  99. /****************************************************************************
  100.  
  101.     FUNCTION:  InitInstance(HANDLE, int)
  102.  
  103.     PURPOSE:  Saves instance handle and creates main window
  104.  
  105. ****************************************************************************/
  106.  
  107. BOOL InitInstance(hInstance, nCmdShow)
  108.     HANDLE          hInstance;
  109.     int             nCmdShow;
  110. {
  111.     HWND            hWnd;
  112.     HDC             hDC;
  113.     TEXTMETRIC      textmetric;
  114.     RECT            rect;
  115.     int             nLineHeight;
  116.  
  117.  
  118.     hInst = hInstance;
  119.  
  120.     hWnd = CreateWindow(
  121.         "InputWClass",
  122.         "Input Sample Application",
  123.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,  /* horz & vert scroll bars */
  124.         CW_USEDEFAULT,
  125.         CW_USEDEFAULT,
  126.         CW_USEDEFAULT,
  127.         CW_USEDEFAULT,
  128.         NULL,
  129.         NULL,
  130.         hInstance,
  131.         NULL
  132.     );
  133.  
  134.     if (!hWnd)
  135.         return (FALSE);
  136.  
  137.  
  138.     hDC = GetDC(hWnd);
  139.     GetTextMetrics(hDC, &textmetric);
  140.     ReleaseDC(hWnd, hDC);
  141.     nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
  142.  
  143.     rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
  144.     rect.right  = GetDeviceCaps(hDC, HORZRES);
  145.     rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
  146.     rect.bottom = rect.top + nLineHeight;
  147.     rectMouse   = rect;
  148.  
  149.     rect.top += nLineHeight;
  150.     rect.bottom += nLineHeight;
  151.     rectButton = rect;
  152.  
  153.     rect.top += nLineHeight;
  154.     rect.bottom += nLineHeight;
  155.     rectKeyboard = rect;
  156.  
  157.     rect.top += nLineHeight;
  158.     rect.bottom += nLineHeight;
  159.     rectCharacter = rect;
  160.  
  161.     rect.top += nLineHeight;
  162.     rect.bottom += nLineHeight;
  163.     rectScroll = rect;
  164.  
  165.     rect.top += nLineHeight;
  166.     rect.bottom += nLineHeight;
  167.     rectTimer = rect;
  168.  
  169.     ShowWindow(hWnd, nCmdShow);
  170.     UpdateWindow(hWnd);
  171.     return (TRUE);
  172.  
  173. }
  174.  
  175. /****************************************************************************
  176.  
  177.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  178.  
  179.     PURPOSE:  Processes messages
  180.  
  181.     MESSAGES:
  182.  
  183.     WM_COMMAND    - application menu (About dialog box)
  184.     WM_CREATE     - create window
  185.         WM_MOUSEMOVE  - mouse movement
  186.         WM_LBUTTONDOWN - left mouse button pressed
  187.         WM_LBUTTONUP  - left mouse button released
  188.         WM_LBUTTONDBLCLK - left mouse button double clicked
  189.         WM_KEYDOWN    - key pressed
  190.         WM_KEYUP      - key released
  191.         WM_CHAR       - ASCII character received
  192.         WM_TIMER      - timer has elapsed
  193.         WM_HSCROLL    - mouse click in horizontal scroll bar
  194.         WM_VSCROLL    - mouse click in vertical scroll bar
  195.         WM_PAINT      - update window, draw objects
  196.         WM_DESTROY    - destroy window
  197.  
  198.     COMMENTS:
  199.  
  200.         This demonstrates how input messages are received, and what the
  201.         additional information is that comes with the message.
  202.  
  203. ****************************************************************************/
  204.  
  205. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  206. HWND hWnd;
  207. unsigned message;
  208. WORD wParam;
  209. LONG lParam;
  210. {
  211.     FARPROC lpProcAbout;
  212.  
  213.     HDC hDC;                         /* display-context variable     */
  214.     PAINTSTRUCT ps;                  /* paint structure              */
  215.     char HorzOrVertText[12];
  216.     char ScrollTypeText[20];
  217.     RECT rect;
  218.  
  219.     switch (message)
  220.     {
  221.         case WM_COMMAND:
  222.             if (wParam == IDM_ABOUT)
  223.             {
  224.                 lpProcAbout = MakeProcInstance(About, hInst);
  225.                 DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  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.     {
  366.         case WM_INITDIALOG:
  367.             return (TRUE);
  368.  
  369.         case WM_COMMAND:
  370.             if (wParam == IDOK)
  371.             {
  372.                 EndDialog(hDlg, TRUE);
  373.                 return (TRUE);
  374.             }
  375.             break;
  376.     }
  377.     return (FALSE);
  378. }
  379.