home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / INPUT / INPUT.C_ / INPUT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  11.3 KB  |  378 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 = COLOR_WINDOW+1;
  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.     if (!hDC)
  137.         return FALSE;
  138.     GetTextMetrics(hDC, &textmetric);
  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.     ReleaseDC(hWnd, hDC);
  145.     rect.bottom = rect.top + nLineHeight;
  146.     rectMouse   = rect;
  147.  
  148.     rect.top += nLineHeight;
  149.     rect.bottom += nLineHeight;
  150.     rectButton = rect;
  151.  
  152.     rect.top += nLineHeight;
  153.     rect.bottom += nLineHeight;
  154.     rectKeyboard = rect;
  155.  
  156.     rect.top += nLineHeight;
  157.     rect.bottom += nLineHeight;
  158.     rectCharacter = rect;
  159.  
  160.     rect.top += nLineHeight;
  161.     rect.bottom += nLineHeight;
  162.     rectScroll = rect;
  163.  
  164.     rect.top += nLineHeight;
  165.     rect.bottom += nLineHeight;
  166.     rectTimer = rect;
  167.     
  168.     ShowWindow(hWnd, nCmdShow);
  169.     UpdateWindow(hWnd);
  170.     return (TRUE);
  171.  
  172. }
  173.  
  174. /****************************************************************************
  175.  
  176.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  177.  
  178.     PURPOSE:  Processes messages
  179.  
  180.     MESSAGES:
  181.  
  182.     WM_COMMAND    - application menu (About dialog box)
  183.     WM_CREATE     - create window
  184.         WM_MOUSEMOVE  - mouse movement
  185.         WM_LBUTTONDOWN - left mouse button pressed
  186.         WM_LBUTTONUP  - left mouse button released
  187.         WM_LBUTTONDBLCLK - left mouse button double clicked
  188.         WM_KEYDOWN    - key pressed
  189.         WM_KEYUP      - key released
  190.         WM_CHAR       - ASCII character received
  191.         WM_TIMER      - timer has elapsed
  192.         WM_HSCROLL    - mouse click in horizontal scroll bar
  193.         WM_VSCROLL    - mouse click in vertical scroll bar
  194.         WM_PAINT      - update window, draw objects
  195.         WM_DESTROY    - destroy window
  196.  
  197.     COMMENTS:
  198.  
  199.         This demonstrates how input messages are received, and what the
  200.         additional information is that comes with the message.
  201.  
  202. ****************************************************************************/
  203.  
  204. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  205. HWND hWnd;
  206. UINT message;
  207. WPARAM wParam;
  208. LPARAM lParam;
  209. {
  210.     FARPROC lpProcAbout;
  211.  
  212.     HDC hDC;                         /* display-context variable     */
  213.     PAINTSTRUCT ps;                  /* paint structure              */
  214.     char HorzOrVertText[12];
  215.     char ScrollTypeText[20];
  216.     RECT rect;
  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.          break;
  240.  
  241.       case WM_MOUSEMOVE:
  242.          wsprintf(MouseText, "WM_MOUSEMOVE: %x, %d, %d",
  243.              wParam, LOWORD(lParam), HIWORD(lParam));
  244.          InvalidateRect(hWnd, &rectMouse, TRUE);
  245.          break;
  246.  
  247.       case WM_LBUTTONDOWN:
  248.          wsprintf(ButtonText, "WM_LBUTTONDOWN: %x, %d, %d",
  249.                 wParam, LOWORD(lParam), HIWORD(lParam));
  250.          InvalidateRect(hWnd, &rectButton, TRUE);
  251.          break;
  252.  
  253.       case WM_LBUTTONUP:
  254.          wsprintf(ButtonText, "WM_LBUTTONUP: %x, %d, %d",
  255.                 wParam, LOWORD(lParam), HIWORD(lParam));
  256.          InvalidateRect(hWnd, &rectButton, TRUE);
  257.          break;
  258.  
  259.       case WM_LBUTTONDBLCLK:
  260.          wsprintf(ButtonText, "WM_LBUTTONDBLCLK: %x, %d, %d",
  261.                 wParam, LOWORD(lParam), HIWORD(lParam));
  262.          InvalidateRect(hWnd, &rectButton, TRUE);
  263.          break;
  264.  
  265.       case WM_KEYDOWN:
  266.           wsprintf(KeyboardText, "WM_KEYDOWN: %x, %x, %x",
  267.               wParam, LOWORD(lParam), HIWORD(lParam));
  268.           InvalidateRect(hWnd, &rectKeyboard, TRUE);
  269.           break;
  270.  
  271.       case WM_KEYUP:
  272.           wsprintf(KeyboardText, "WM_KEYUP: %x, %x, %x",
  273.               wParam, LOWORD(lParam), HIWORD(lParam));
  274.           InvalidateRect(hWnd, &rectKeyboard, TRUE);
  275.           break;
  276.  
  277.       case WM_CHAR:
  278.           wsprintf(CharacterText, "WM_CHAR: %c, %x, %x",
  279.               wParam, LOWORD(lParam), HIWORD(lParam));
  280.           InvalidateRect(hWnd, &rectCharacter, TRUE);
  281.           break;
  282.  
  283.       case WM_TIMER:
  284.           wsprintf(TimerText, "WM_TIMER: %d seconds",
  285.               nTimerCount += 5);
  286.           InvalidateRect(hWnd, &rectTimer, TRUE);
  287.           break;
  288.  
  289.       case WM_HSCROLL:
  290.       case WM_VSCROLL:
  291.           lstrcpy(HorzOrVertText,
  292.               (message == WM_HSCROLL) ? "WM_HSCROLL" : "WM_VSCROLL");
  293.           lstrcpy(ScrollTypeText,
  294.               (wParam == SB_LINEUP) ? "SB_LINEUP" :
  295.               (wParam == SB_LINEDOWN) ? "SB_LINEDOWN" :
  296.               (wParam == SB_PAGEUP) ? "SB_PAGEUP" :
  297.               (wParam == SB_PAGEDOWN) ? "SB_PAGEDOWN" :
  298.               (wParam == SB_THUMBPOSITION) ? "SB_THUMBPOSITION" :
  299.               (wParam == SB_THUMBTRACK) ? "SB_THUMBTRACK" :
  300.               (wParam == SB_ENDSCROLL) ? "SB_ENDSCROLL" : "unknown");
  301.           wsprintf(ScrollText, "%s: %s, %x, %x",
  302.               (LPSTR)HorzOrVertText,
  303.               (LPSTR)ScrollTypeText,
  304.               LOWORD(lParam),
  305.               HIWORD(lParam));
  306.           InvalidateRect(hWnd, &rectScroll, TRUE);
  307.           break;
  308.  
  309.       case WM_PAINT:
  310.           hDC = BeginPaint (hWnd, &ps);
  311.  
  312.           if (IntersectRect(&rect, &rectMouse, &ps.rcPaint))
  313.               TextOut(hDC, rectMouse.left, rectMouse.top, 
  314.                       MouseText, lstrlen(MouseText));
  315.           if (IntersectRect(&rect, &rectButton, &ps.rcPaint))
  316.               TextOut(hDC, rectButton.left, rectButton.top, 
  317.                       ButtonText, lstrlen(ButtonText));
  318.           if (IntersectRect(&rect, &rectKeyboard, &ps.rcPaint))
  319.               TextOut(hDC, rectKeyboard.left, rectKeyboard.top, 
  320.                       KeyboardText, lstrlen(KeyboardText));
  321.           if (IntersectRect(&rect, &rectCharacter, &ps.rcPaint))
  322.               TextOut(hDC, rectCharacter.left, rectCharacter.top, 
  323.                       CharacterText, lstrlen(CharacterText));
  324.           if (IntersectRect(&rect, &rectTimer, &ps.rcPaint))
  325.               TextOut(hDC, rectTimer.left, rectTimer.top, 
  326.                       TimerText, lstrlen(TimerText));
  327.           if (IntersectRect(&rect, &rectScroll, &ps.rcPaint))
  328.               TextOut(hDC, rectScroll.left, rectScroll.top, 
  329.                       ScrollText, lstrlen(ScrollText));
  330.  
  331.           EndPaint(hWnd, &ps);
  332.           break;
  333.  
  334.       case WM_DESTROY:
  335.           KillTimer(hWnd, idTimer);                     /* Stops the timer */
  336.           PostQuitMessage(0);
  337.           break;
  338.  
  339.     default:
  340.         return (DefWindowProc(hWnd, message, wParam, lParam));
  341.     }
  342.     return (NULL);
  343. }
  344.  
  345.  
  346. /****************************************************************************
  347.  
  348.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  349.  
  350.     PURPOSE:  Processes messages for "About" dialog box
  351.  
  352.     MESSAGES:
  353.  
  354.     WM_INITDIALOG - initialize dialog box
  355.     WM_COMMAND    - Input received
  356.  
  357. ****************************************************************************/
  358.  
  359. BOOL FAR PASCAL __export About(hDlg, message, wParam, lParam)
  360. HWND hDlg;
  361. unsigned message;
  362. WORD wParam;
  363. LONG lParam;
  364. {
  365.     switch (message) {
  366.        case WM_INITDIALOG:
  367.           return (TRUE);
  368.  
  369.        case WM_COMMAND:
  370.           if (wParam == IDOK) {
  371.               EndDialog(hDlg, TRUE);
  372.               return (TRUE);
  373.           }
  374.           break;
  375.     }
  376.     return (FALSE);
  377. }
  378.