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